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?
-
@T.J.Crowder no it dont have its ownMaizere Pathak– Maizere Pathak2013年01月28日 05:29:04 +00:00Commented Jan 28, 2013 at 5:29
-
@MaizerePathak: It does for me: i.imgur.com/ENMQfJQ.pngBlender– Blender2013年01月28日 05:29:44 +00:00Commented Jan 28, 2013 at 5:29
-
@Blender i said it dont have its own.That construter is inherited from its internal prototype whose value is set to Object.prototype .so from here it is inheritedMaizere Pathak– Maizere Pathak2013年01月28日 05:31:25 +00:00Commented Jan 28, 2013 at 5:31
-
@MaizerePathak: Proper spelling, punctuation, capitalization, and spacing help people understand what you're trying to say. We have those things for a reason.T.J. Crowder– T.J. Crowder2013年01月28日 05:50:20 +00:00Commented Jan 28, 2013 at 5:50
1 Answer 1
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`.
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.