What is the difference between using instanceof and constructor validation?

Why do the next two lines return different results?

("test" instanceof String) // returns false ("test".constructor == String) // returns true 

Tested in the console chrome version 28.0.1500.95 m

Does it work a little differently for native types?

+7
javascript
source share
2 answers

constructor is simply a property of the [[prototype]] internal property that is easy to manipulate:

 function A(){} function B(){} A.prototype.constructor = B; var a = new A(); console.log(a.constructor); //B 

However, the instanceof operator checks the inner prototype chain and is not so easy to fool, even if you change the full prototype property of the constructor function:

 function A(){} function B(){} A.prototype = B.prototype; var a = new A(); console.log(a instanceof A); //true console.log(a instanceof B); //false 

So, why is "test" instanceof String === false , but ("test".constructor == String) === true ?

First of all, "test" is primitive, and primitives are never an example of anything. In fact, when you use instanceof , the constructor's [[HasInstance]] internal method is called with a possible instance as an argument. So, a instanceof A roughly translates:

 `A.[[HasInstance]](a)` 

ECMA specifications have this meaning to say [[HasInstance]] : http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.3

[[HasInstance]] (V)

Suppose F is a Function object.

When the [[HasInstance]] F internal method is called with a value of V, the following steps are performed:

  • If V is not an object, return false.
  • ....

In other words: if the left side of instanceof not an object, the statement will return false.

("test".constructor == String) === true works for another reason: if you try to access the property of the primitive, the primitive will be temporarily converted to an object. So, "test".constructor approximately equal to:

 (new String("test")).constructor 

in this case, you are actually creating an object with a constructor function and requesting the constructor property. Therefore it is not surprising that it will return String .

+4
source share

The main difference is that instanceof checks the prototype chain of the object, while constructor checking only checks if it was created from the same constructor.

Example:

 function MyObject() { this.sayHi = function() { alert('Hi!'); } } var x = new MyObject(); alert(x.constructor === Object); alert(x instanceof Object); 
+1
source share

All Articles