Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^3: Outputting JSON with sorted names/keys (JS Object vs Perl hash / updated)

by LanX (Cardinal)
on Jan 27, 2020 at 12:37 UTC ( #11111926=note: print w/replies, xml ) Need Help??

in reply to Re^2: Outputting JSON with sorted names/keys
in thread Outputting JSON with sorted names/keys

> An object is an unordered set of name/value pairs.

To make it worse, I'm pretty sure that in JS this "object" should be written capitalized as "Object".

"Object" is the base "object" instance in JS from which all other objects inherit°, including the objects Array and Function. A literal "Object" is written with key value pairs surrounded by curlies. "object" is a type.

From a Perl perspective objects are best understood as tied hashes which lookup missing keys via the "prototype" chain (exposed in FF thru __proto__ ).

I'm starting to agree that Crockford should have avoided the term "object" in his JSON definition...


Demo from the FF console (please note that __proto__ is not necessarily available in other dialects)

> hash = {a:1} Object { a: 1 } > typeof hash "object" > array = [0,1,2] Array(3) [ 0, 1, 2 ] > typeof array "object" > array.__proto__ Array [] > array.__proto__.__proto__ Object { … }


> h={} // empty hash? Object { } > h["constructor"] // well, beware of inherited attributes function Object()


°) seems like newer ECMA versions allow the creation of objects without prototype. This would actually be very close to a Perl hash.

> h = Object.create(null) Object { } > h["constructor"] undefined

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11111926]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-07-07 19:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found