hasOwnProperty and isprototypeof in JavaScript

Source: Internet
Author: User
Tags hasownproperty

hasOwnProperty: is used to determine whether an object has a property or object that you give a name to. However, it is important to note that this method cannot check whether the object has this property in its prototype chain, and that the property must be a member of the object itself.
isPrototypeOf is used to determine whether the object whose prototype chain is to be checked exists in the specified object instance, or True, otherwise returns false.

functionSiteAdmin (nickname,sitename) { This. nickname=nickname;  This. sitename=SiteName; } siteAdmin.prototype.showAdmin=function() {alert ( This. nickname+ "Yes" + This. sitename+ "the webmaster!")        }; SiteAdmin.prototype.showSite=function(siteurl) { This. siteurl=SiteURL; return  ThisThe address of the. sitename+ "is" + This. SiteURL;        }; varmatou=NewSiteAdmin ("Fool's Wharf", "Web front-end development"); varMatou2=NewSiteAdmin ("Fool's Wharf", "Web front-end development"); Matou.age= "30";//matou.showadmin ();//Alert (Matou.showsite ("http://www.css88.com/"));Alert (Matou.hasownproperty ("nickname"));//trueAlert (Matou.hasownproperty ("Age"));//trueAlert (Matou.hasownproperty ("showadmin"));//falseAlert (Matou.hasownproperty ("SiteURL"));//falseAlert (SiteAdmin.prototype.hasOwnProperty ("showadmin"));//trueAlert (SiteAdmin.prototype.hasOwnProperty ("SiteURL"));//falseAlert (SiteAdmin.prototype.isPrototypeOf (Matou))//trueAlert (SiteAdmin.prototype.isPrototypeOf (MATOU2))//true

1 for in can get all the properties of an object, including custom attributes and prototype chain properties.

for (var attr in object) {

Console.log (attr+ ":" object[attr]);

}

2 hasOwnProperty () can only get custom properties and cannot get prototype chain properties.

"Str". hasOwnProperty ("split");//false

String.property.hasOwnProperty ("split");//true

hasOwnProperty and isprototypeof in JavaScript

Related Article

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.