hasOwnProperty (), propertyisenumerable () and isprototypeof () _javascript techniques in JS

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.