Skip to content

re: Latest thing I learned about Object and friends VIEW POST

re: Then debunk this: Object.constructor() // f anonymous() {} Object.constructor("return 2")() // 2 Object.constructor === Function.constructor // t...


Okay, I'm puzzled here. It seems like objects are functions. And in a certain sense, they are... The key is the argument and the value is the return value. But how does-- OMG! It's like there's a single method for accessing the properties of an object, and getters just override the behavior for a certain argument. But it's not completely the case, because Function != Object.

Interestingly, Function == Function.constructor, but Object != Object.constructor.

I've also noticed that Object.keys returns [] for an int or boolean, so I don't think too much of that Object.keys and the others return [] for Object.

Also, LOL at this:

> typeof null
> Object.keys(null)
TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)

Wait what the hell! Function.keys?!? But I tried Function.keys and it doesn't exist!

Wait a minute...

> Number.constructor
[Function: Function]
> Number.constructor == Object.constructor
> Number.constructor == Function.constructor

All types' constructors are Function's constructor.

Even non-primitive ones, like HMTLElement.

Okay, I think I figured out the constructor thing. An object's constructor is the thing that constructed it. So, since Object is the constructor for objects, it is therefore a function, and Function is its constructor.

I'm not even sure if I've solved it or not...


I'm pretty sure I did and it was all the same results... == and === both compare by identity for objects and functions. Or am I wrong about that too?

Well you're right, it's just a better practice to use '===', it's a 'safety mechanism' in JS



> f = k => k*2
[Function: f]
> f.keys = k => k.v
[Function (anonymous)]
> f.keys()
TypeError: Cannot read property 'v' of undefined
    at Function.f.keys (repl:1:17)

Function.f.keys?!? But when I did this with Object, it was just Function.keys, not Function.Object.keys.

On that note:

> Object.keys
[Function: keys]

This would seem to imply that keys exists in the global scope, which it doesn't. When I tried this on f.keys it was "[Function: (anonymous)]".

JavaScript is really weird...
But there is one more question:
Then what does

a = {}
b = {Key: "value", foo: "bar"}

do? Is there any equivalent in plain JS?
There is still so much more we need to know...
But good investigations so far, I'm also working on this as well!

code of conduct - report abuse