I am seriously considering the ECMAScript 5.1 specification.
And I'm curious if there is a way to tell what the "real design function" of an object is in ES5. (not explicit property of "constructor")
According to the specification, the explicit property "constructor" is simply the initial backlink from the object of the side object of the function object to the function object.
(Here, a by-product is an object that is initially indicated by the explicit property "prototype" of the function object).
So, the explicit property of "constructor" has nothing to do with the real constructor of the object:
function Foo(name){ this.name = name; } var foo = new Foo("Brian"); var bar = { a: 10, b: function(){ if(this.name) return this.name; }}; Foo.prototype = bar; var foo2 = new Foo("John");
Here, although foo2 was created by Foo, since Foo.prototype was pointing to bar at the time of foo2 creation, so foo2 internal [[Prototype]] points to bar, and we can check this for:
Object.getPrototypeOf(foo2) === bar
Since foo2 does not have its own “constructor” property, as usual, so reading foo2.constructor is actually a [[Prototype]] chain search, that is, foo2.constructor → ((foo2. [[Prototype]])). constructor -> bar.constructor, which is an Object, not a Foo.
You can check this out:
foo2.constructor === Foo // false foo2.constructor === Object // true
Finally, there is no way to find Foo from a foo2 object?
ADD: I'm talking about ES5. Please do not bring anything engine or ES6-thingy dependent (e.g. __proto__ )