First learn about enumeration properties
General use of For~in traversal
var a = [1,2,3];
for (var i in a) {
console.log (a[i]);
}
or
var 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. such as the Length property (array) and the constructor property. The attributes that have been shown are called enumerable and can be judged by the propertyisenumerable () method provided by each object to determine which enumerable attributes are available;
Each attribute in the <2> prototype chain is also displayed, provided they are enumerable, hasownproperty () to determine whether an attribute is an object's own property or a prototype property;
<3> for all prototype properties, propertyIsEnumerable () returns false, including those that can be enumerated in 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=3
newdog.hasownproperty (' name ');//true;
Newdog.hasownproperty (' price ');//false;
Show only its own properties
For (Var prop in Newdog) {
if (Newdog.hasownproperty (prop)) {
Console.log (prop+ ' = ' +newdog[prop]);
}
newdog.propertyisenumerable (' name ');//true
newdog.propertyisenumerable (' constructor ');//false
Note: Most of the built-in properties and methods are not enumerable
Any attribute from the prototype chain is also not enumerable
If the call to propertyIsEnumerable () is from an object on the prototype chain, the property in that object is enumerable
NewDog.constructor.prototype.propertyIsEnumerable (' price ');//true
isPrototypeOf (): Each object has a prototype that indicates whether the current object is 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);//true
The above is a small set to introduce the JS in the hasOwnProperty (), propertyisenumerable () and isprototypeof (), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!