Object-oriented programming judgment method

instanceof: examines the relationship between prototype objects and instance objects

For example: alert(cat1 instanceof Cat); //true

   alert(cat2 instanceof Cat); //true

 Cat is the prototype object, cat1 and cat2 are instance objects.

isPrototypeOf(): Determine the relationship between an proptotypeobject and an instance.

 

alert(Cat.prototype.isPrototypeOf(cat1)); //true

 proptotypeObject: Every constructor has a prototypeproperty that points to another object. All properties and methods are inherited by the instance of the constructor.

hasOwnProperty(): Determine the origin of a property (it can be a local property or a property of an inherited prototypeobject. )

alert(cat1.hasOwnProperty("type")); // false

 in operator

1) Determine whether an instance contains an attribute

alert("name" in cat1); // true

alert("type" in cat1); // true

 2) Iterate over all properties of an object

for(var prop in cat1) { alert("cat1["+prop+"]="+cat1[prop]); }

 Here are just some methods selected, and the specific usage needs to be used flexibly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325975108&siteId=291194637
Recommended