Home  >  Article  >  Web Front-end  >  Examples of using hasOwnProperty and isPrototypeOf methods in js_javascript skills

Examples of using hasOwnProperty and isPrototypeOf methods in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:45:331142browse

hasOwnProperty: is used to determine whether an object has a property or object with the name you gave it. However, it should be noted that this method cannot check whether the object has the property in the prototype chain. 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. If so, it returns true, otherwise it returns false.

Copy code The code is as follows:

function siteAdmin(nickName,siteName){
this .nickName=nickName;
this.siteName=siteName;
}
siteAdmin.prototype.showAdmin = function() {
alert(this.nickName "is the webmaster of " this.siteName "! ")
};
siteAdmin.prototype.showSite = function(siteUrl) {
this.siteUrl=siteUrl;
return this.siteName "The address is " this.siteUrl;
} ;
var matou=new siteAdmin("Script Home","WEB Front-end Development");
var matou2=new siteAdmin("Script Home","WEB Front-end Development");
matou. age="30";
// matou.showAdmin();
// alert(matou.showSite("http://www.jb51.net/"));
alert(matou. hasOwnProperty("nickName"));//true
alert(matou.hasOwnProperty("age"));//true
alert(matou.hasOwnProperty("showAdmin"));//false
alert(matou.hasOwnProperty("siteUrl"));//false
alert(siteAdmin.prototype.hasOwnProperty("showAdmin"));//true
alert(siteAdmin.prototype.hasOwnProperty("siteUrl") );//false
alert(siteAdmin.prototype.isPrototypeOf(matou))//true
alert(siteAdmin.prototype.isPrototypeOf(matou2))//true
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn