Consider the following code.
function a() {} function b() {} function c() {} b.prototype = new a(); c.prototype = new b(); console.log((new a()).constructor); //a() console.log((new b()).constructor); //a() console.log((new c()).constructor); //a()
Further, please consider the following.
console.log(new a() instanceof a); //true console.log(new b() instanceof b); //true console.log(new c() instanceof c); //true
(new c()).constructor
is equal to a()
and Object.getPrototypeOf(new c())
is a{ }
, how is it possible for instanceof
to know that new c()
is an instance of c
?http://jsfiddle.net/ezZr5/
The constructor property returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function's name. The value is only read-only for primitive values such as 1 , true , and "test" .
Inheritance enables you to define a class that takes all the functionality from a parent class and allows you to add more. Using class inheritance, a class can inherit all the methods and properties of another class. Inheritance is a useful feature that allows code reusability.
Okay, let's play a little mind game:
From the above image we can see:
function Foo() {}
, JavaScript creates a Function
instance.Function
instance (the constructor function) has a property prototype
which is a pointer.prototype
property of the constructor function points to its prototype object.constructor
which is also a pointer.constructor
property of the prototype object points back to its constructor function.Foo
like new Foo()
, JavaScript creates a new object.[[proto]]
property of the instance points to the prototype of the constructor.Now, the question arises that why doesn't JavaScript attach the constructor
property to the instance object instead of the prototype. Consider:
function defclass(prototype) { var constructor = prototype.constructor; constructor.prototype = prototype; return constructor; } var Square = defclass({ constructor: function (side) { this.side = side; }, area: function () { return this.side * this.side; } }); var square = new Square(10); alert(square.area()); // 100
As you can see the constructor
property is just another method of the prototype, like area
in the example above. What makes the constructor
property special is that it's used to initialize an instance of the prototype. Otherwise it's exactly the same as any other method of the prototype.
Defining the constructor
property on the prototype is advantageous for the following reasons:
Object.prototype
. The constructor
property of Object.prototype
points to Object
. If the constructor
property was defined on the instance then Object.prototype.constructor
would be undefined
because Object.prototype
is an instance of null
.new
easier since it doesn't need to define the constructor
property on every instance.constructor
property. Hence it's efficient.Now when we talk about inheritance, we have the following scenario:
From the above image we can see:
prototype
property is set to the instance of the base constructor.[[proto]]
property of the instance of the derived constructor points to it too.constructor
property of the derived constructor instance now points to the base constructor.As for the instanceof
operator, contrary to popular belief it doesn't depend on the constructor
property of the instance. As we can see from above, that would lead to erroneous results.
The instanceof
operator is a binary operator (it has two operands). It operates on an instance object and a constructor function. As explain on Mozilla Developer Network, it simply does the following:
function instanceOf(object, constructor) { while (object != null) { if (object == constructor.prototype) { //object is instanceof constructor return true; } else if (typeof object == 'xml') { //workaround for XML objects return constructor.prototype == XML.prototype; } object = object.__proto__; //traverse the prototype chain } return false; //object is not instanceof constructor }
To put it simply if Foo
inherits from Bar
, then the prototype chain for the instance of Foo
would be:
foo.__proto__ === Foo.prototype
foo.__proto__.__proto__ === Bar.prototype
foo.__proto__.__proto__.__proto__ === Object.prototype
foo.__proto__.__proto__.__proto__.__proto__ === null
As you can see, every object inherits from the Object
constructor. The prototype chain ends when an internal [[proto]]
property points to null
.
The instanceof
function simply traverses the prototype chain of the instance object (the first operand) and compares the internal [[proto]]
property of each object to the prototype
property of the constructor function (the second operand). If they match, it returns true
; and else if the prototype chain ends, it returns false
.
By default,
function b() {}
then b.prototype
has a .constructor
property which is set to b
automatically. However, you're currently overwriting the prototype and thus discarding that variable:
b.prototype = new a;
Then b.prototype
does not have a .constructor
property anymore; it was erased with the overwrite. It does inherit from a
though, and (new a).constructor === a
, and hence (new b).constructor === a
(it is referring to the same property in the prototype chain).
Best to do is to simply setting it manually afterwards:
b.prototype.constructor = b;
You could also make a little function for this:
function inherit(what, from) { what.prototype = new from; what.prototype.constructor = what; }
http://jsfiddle.net/79xTg/5/
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