Maybe silly question to the JS gurus and ninjas out there but here goes:
My understanding of the prototype object/property of an object is that it is a blueprint for future instances of an object. Given this, shouldn’t a newly created instance of an object be identical to the constructor object that created it?
var x = new Object();
console.log(x === Object.prototype); // returns false. why??
* UPDATE *
So understanding that this will return false because they are referencing different things, I still find that new Object() and Object.prototype contain a different number of properties. So to refine my question: How do I correctly check the number of properties in a prototype Object; how do I iterate through them?
The reason I got confused by this is that if I create a simple constructor function:
function Circle(){
this.tail = "yes, has tail";
}
and want to get the number of properties it has, doing something like:
console.log(Object.getOwnPropertyNames(Circle.prototype));
// returns "constructor", I expected it to return "tail"
===does not answer the question of whether two things are equivalent, but whether they are references to the same object.xandObject.prototypein your example may have the same properties, so you can call them equivalent, but they are two different objects.If you do
they are now no longer equivalent, because they were two different objects. You changed one but not the other.
If
were true, then
would be the same regardless of what you assign to
x.fooorObject.prototype.foo.EDIT:
There is no
tailproperty onCircle.prototypebecause you have never doneCircle.prototype.tail = ...;. You definetailonly onCircleinstances viathis.tail = ...;.You are also doing
getOwnPropertyNames. The own properties are those that are not inherited from the prototype, so by using that function onxyou are explicitly excluding all the properties ofObject.prototype.The docs for
hasOwnPropertyexplain "own property" pretty well: