In the book Secrets of the JavaScript Ninja, 2013, page 125, it says:
Each object in JavaScript has an implicit property named
constructor
that references the constructor that was used to create the object. And because the prototype is a property of the constructor, each object has a way to find its prototype.
It actually might be one of the most flawed things I heard about JavaScript, and it came from a supposedly expert of JavaScript. Isn't it true that
any JavaScript object "has a way to find its prototype" using the internal [[prototype]]
property (as in the ECMA-262 specs, page 32). It is accessible on Chrome and Firefox using __proto__
, and in more recently versions of IE, using Object.getPrototypeOf
any object gets to the constructor
property by getting it in the prototype object that __proto__
is pointing to. The constructor
property is sometimes even not set correctly as some JavaScript libraries or frameworks don't use it at all. constructor
is a property of the prototype object, not a property of the object itself:
(as seen in Chrome's developer's tool):
> function Foo() {}
undefined
> var foo = new Foo()
undefined
> foo.hasOwnProperty("constructor")
false
> foo.__proto__.hasOwnProperty("constructor")
true
> foo.__proto__.constructor === Foo
true
Is the above (1) and (2) true? What is the "implicit property named constructor
" in JavaScript as in the quoted text? Does it try to mean something like [[prototype]]
which is an internal property? But more importantly, I'd like to know whether (1) and (2) above are true and not what the quoted text says it is.
The quoted text is very accurate and explains this mechanism very simply.
"Each object in JavaScript has an implicit property named constructor that references the constructor that was used to create the object."
This is absolutely true, as @Mathletics pointed out:
foo.constructor === Foo // true
..."And because the prototype is a property of the constructor, each object has a way to find its prototype."
This also can be understood plainly as read. Getting the prototype from the constructor is a valid way for an instance to find its prototype.
foo.constructor.prototype // Foo {}
And also
foo.constructor.prototype === foo.__proto__ // true
I think that the way the book describes it, is the most proper one of doing it. The "__proto__" property is named with double underscore on each side for a reason. As you pointed out, it is an internal property, and the double underscore is a widely used convention for naming internal properties. It is not "visible" with hasOwnProperty. Not specifically because it is an internal property, but because it is not set directly on the object itself. This might better explain what hasOwnPropery means more clearly:
foo.a = 4;
foo.a; // 4
foo.hasOwnProperty("a"); // true
foo.constructor.prototype.b = 5;
foo.b; // 5
foo.hasOwnProperty("b"); // false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With