Maizere Pathak
Maizere Pathak

Reputation: 299

javascript:Object instance and prototype

protype is an object whose internal prototype property is set to Object.prototype and has constructor property in it.but when we do this:

x=new Object;//

x now dont have its own constructor property why?since prototype are also the instance of function Object but they have constructor property.

My question is since prototypes are also the instance of the Object function and empty object({}) are also the instance of the Object function ,one lacks the property called constructor and one has.Why is so?

Upvotes: 0

Views: 244

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075567

Before your edit

x now dont have constructor property

Yes, it does. It inherits it from Object.prototype.

This code:

var x = new Object();
console.log("typeof x.constructor = " + typeof x.constructor);

...outputs typeof x.constructor = function. Live Example | Source

After your edit

x now dont have its own constructor property

(My emphasis)

It doesn't have its own property called constructor (x.hasOwnProperty("constructor") is false) because it doesn't have any reason to have it, it's on the prototype.

It's important to realize that the link between an object and its prototype is a live link. The object doesn't get a copy of the prototype, it gets a reference to it. When we retrieve a property from an object, if the object doesn't have its own property with that name, the reference to the prototype is followed to see if the prototype has that property (and so on up the prototype chain all the way to Object.prototype). This is the whole point of prototypical inheritance, objects don't have copies of the properties of their prototypes, they inherit from their prototypes.

Here's an example of how it's a live link:

function Foo() {
}
Foo.prototype.answer = 42;

var f = new Foo();
console.log("[before] f.question = " + f.question);
console.log("[before] f.answer = " + f.answer);

// Note that `f.question` is undefined, of course, as we haven't defined it

Foo.prototype.question = "Life, the Universe, and Everything";

console.log("[after] f.question = " + f.question);
console.log("[after] f.answer = " + f.answer);

// Note that `f.question` is defined now, even thoguh `f` was created
// BEFORE we added that to the `Foo.prototype`.

Live Example | Source


Re your comment:

My question is since prototypes are also the instance of the Object function and empty object({}) are also the instance of the Object function ,one lacks the property called constructor and one has.Why is so?

Because only the object assigned to Object.prototype has actually been assigned the constructor property. This is part of creating a function (in this case Object, but it's true of any function). See Step #17 of §13.2 of the specification. All other objects created (literally or otherwise) via new Object receive the property via the prototype chain, but the specific object on Object.prototype has had that property directly assigned to it.

Upvotes: 2

Related Questions