IsPrototypeOf and the difference between instanceof

IsPrototypeOf and read a lot about the difference between instanceOf feeling down or carefully understand more clearly presented on MDN:

They do the same thing, two traverse the prototype chain looking for specific objects within them

isPrototypeOf() The method used to test whether an object is present on another object prototype chain.

 
Syntax: prototypeObj.isPrototypeOf (object) 
Parameters: object search on the prototype chain of the object 
Return value: Boolean, indicating whether the calling object in the prototype chain of another object. 

Description: isPrototypeOf method allows you to check whether an object present on another object prototype chain. 

For example, consider the following prototype chain: 

function Fee () { 
  //... 
} 

Function Fi () { 
  //... 
} 
Fi.prototype new new Fee = (); 

function Fo () { 
  //... 
} 
Fi = new new Fo.prototype (); 

function Fum () { 
  //... 
} 
Fum.prototype new new Fo = (); 
create a Fum following examples, Fi.prototype detecting the presence or absence of the chain on the prototype example. 

Fum new new FUM = var (); 
... 

IF (Fi.prototype.isPrototypeOf (FUM)) { 
  // do something Safe 
}
 

instanceof Operator is used to test whether an object exists in its constructor a prototype chain  prototype properties, is an operator, it is desirable to two operands, a target and a the Constructor function , transfer function prototype properties it is present in the test chain on the object (through [[HasInstance]](V)internal operation, the function is only available in the object).

 
Syntax: the instanceof object constructor 

parameters: Object The object to be detected is a constructor constructor 

Description: the instanceof operator is used to detect the presence or absence in the prototype constructor.prototype chain of object parameters. 
// define a constructor 
function C () {} 
function D () {} 

var new new C = O (); 

// to true, because Object.getPrototypeOf (O) === C.prototype 
O the instanceof C; 

// to false, since the prototype chain D.prototype not o, 
o the instanceof D; 

o the instanceof Object; // to true, because Object.prototype.isPrototypeOf (o) returns to true 
C.prototype the instanceof Object to true //, supra 

C.prototype = {}; 
new new O2 = C var (); 

O2 the instanceof C; to true // 

o the instanceof C; // to false, C.prototype points to an empty object, the object is not the empty prototype chain o. 

D.prototype new new C = () ; // inherit  
var o3 = new D ();
o3 instanceof D; // true 
o3 instanceof C; // true 
Note that, if the expression returns true obj instanceof Foo, it does not mean that the expression will always return true because the value of property has the potential to Foo.prototype will change the value after the change is likely not present on the prototype chain obj, then the value of the original expression becomes false. 
Also one case, the value of the original expression will change the situation is to change the prototype chain of the object obj, although in the current ES specification, we can only read the object's prototype does not change it, but by means of non standard __proto__ magical properties, can be achieved. Eg after obj .__ proto__ = {}, obj instanceof Foo will return a false.
 

Savor will find the main difference between the two is:

That is the instanceof X Y is judged whether X in the prototype prototype chain of Y, we know example prototype chain (the __proto__) is directed prototype its constructor, i.e. the instanceof X Y X Y is judged whether the one example (examples of X, if Y is, examples that he is the parent class X) 
and X.isPrototypeOf (Y) determines whether the object is an X-Y of the chain on the prototype, the same inheritance relationship X Y X is the object chain prototype object Y, i.e. X.isPrototypeOf (Y) X determines whether to inherit Y.

They do the same thing, two traverse the prototype chain looking for specific objects within them

isPrototypeOf() The method used to test whether an object is present on another object prototype chain.

 
Syntax: prototypeObj.isPrototypeOf (object) 
Parameters: object search on the prototype chain of the object 
Return value: Boolean, indicating whether the calling object in the prototype chain of another object. 

Description: isPrototypeOf method allows you to check whether an object present on another object prototype chain. 

For example, consider the following prototype chain: 

function Fee () { 
  //... 
} 

Function Fi () { 
  //... 
} 
Fi.prototype new new Fee = (); 

function Fo () { 
  //... 
} 
Fi = new new Fo.prototype (); 

function Fum () { 
  //... 
} 
Fum.prototype new new Fo = (); 
create a Fum following examples, Fi.prototype detecting the presence or absence of the chain on the prototype example. 

Fum new new FUM = var (); 
... 

IF (Fi.prototype.isPrototypeOf (FUM)) { 
  // do something Safe 
}
 

instanceof Operator is used to test whether an object exists in its constructor a prototype chain  prototype properties, is an operator, it is desirable to two operands, a target and a the Constructor function , transfer function prototype properties it is present in the test chain on the object (through [[HasInstance]](V)internal operation, the function is only available in the object).

 
Syntax: the instanceof object constructor 

parameters: Object The object to be detected is a constructor constructor 

Description: the instanceof operator is used to detect the presence or absence in the prototype constructor.prototype chain of object parameters. 
// define a constructor 
function C () {} 
function D () {} 

var new new C = O (); 

// to true, because Object.getPrototypeOf (O) === C.prototype 
O the instanceof C; 

// to false, since the prototype chain D.prototype not o, 
o the instanceof D; 

o the instanceof Object; // to true, because Object.prototype.isPrototypeOf (o) returns to true 
C.prototype the instanceof Object to true //, supra 

C.prototype = {}; 
new new O2 = C var (); 

O2 the instanceof C; to true // 

o the instanceof C; // to false, C.prototype points to an empty object, the object is not the empty prototype chain o. 

D.prototype new new C = () ; // inherit  
var o3 = new D ();
o3 instanceof D; // true
// true; o3 instanceof C 
values should be noted that, if the expression obj instanceof Foo returns true, it does not mean that the expression will always return true because the value of the property Foo.prototype may change, after the change most likely not present on the prototype chain obj, then the value of the original expression becomes false. 
Also one case, the value of the original expression will change the situation is to change the prototype chain of the object obj, although in the current ES specification, we can only read the object's prototype does not change it, but by means of non standard __proto__ magical properties, can be achieved. Eg after obj .__ proto__ = {}, obj instanceof Foo will return a false.
 

Savor will find the main difference between the two is:

That is the instanceof X Y is judged whether X in the prototype prototype chain of Y, we know example prototype chain (the __proto__) is directed prototype its constructor, i.e. the instanceof X Y X Y is judged whether the one example (examples of X, if Y is, examples that he is the parent class X) 
and X.isPrototypeOf (Y) determines whether the object is an X-Y of the chain on the prototype, the same inheritance relationship X Y X is the object chain prototype object Y, i.e. X.isPrototypeOf (Y) X determines whether to inherit Y.

Guess you like

Origin www.cnblogs.com/yu412/p/11550951.html