First understand the enum properties
Generally, use for~in traversal
var a = [1,2,3];for(var i in a){console.log(a[i]);}orvar o = {p1:1,p2:2};for(var i in o){console.log(i+'='+o[i]);}//p1=1;p2=2;<1> Not all attributes will be displayed in the for~in traversal. For example (array) length attribute and constructor attribute. Those properties that have been displayed are called enumerables, and the properties that are enumerables can be determined by the propertyIsEnumerable() method provided by each object;
<2> Each attribute in the prototype chain will also be displayed, provided that they are enumerable, hasOwnProperty() to determine whether a property is an object's own property or a prototype property;
<3> For all prototype properties, propertyIsEnumerable() will return false, including those enumerable in the for ~in traversal.
js code example
function dog(name,color){this.name = name;this.color = color;this.someMethod = function(){return 1;}}dog.prototype.price=100;dog.prototype.rating=3;var newDog = new dog("doggg","yellow");for(var prop in newDog){console.log(prop+'='+newDog[prop]);}//name=doggg//color=yellow//someMethod=function (){return 1;}//price=100//rating=3newDog.hasOwnProperty('name');//true;newDog.hasOwnProperty('price');//false;Show only its own attributes
for(var prop in newDog){if(newDog.hasOwnProperty(prop )){console.log(prop+'='+newDog[prop]);}}newDog.propertyIsEnumerable('name');//truenewDog.propertyIsEnumerable('constructor');//falseNote: Most of the built-in properties and methods are not enumerable.
Any attributes from the prototype chain are also not enumerable
If the call to propertyIsEnumerable() is from an object on the prototype chain, then the properties in that object are enumerable
newDog.constructor.prototype.propertyIsEnumerable('price');//trueisPrototypeOf(): Each object has it, indicating whether the current object is a prototype of another object
js code example
var monkey = {hair:true,feeds:'bananas',breathes:'air'};function Human(name){this.name = name;}Human.prototype = monkey;var george = new Human('George');monkey.isPrototypeOf(george);//trueThe above is the hasOwnProperty(), propertyIsEnumerable() and isPrototypeOf() in JS introduced to you by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!