The hasOwnProperty and isPrototypeOf methods in js use instances

  • 2020-03-30 03:12:29
  • OfStack

HasOwnProperty: Is used to determine whether an object has a property or object that you give the name to. It is important to note, however, that this method cannot check whether the property is in the prototype chain of the object, which 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, and returns true or false.


function siteAdmin(nickName,siteName){
 this.nickName=nickName;
 this.siteName=siteName;
}
siteAdmin.prototype.showAdmin = function() {
 alert(this.nickName+" is "+this.siteName+" The master of !")
};
siteAdmin.prototype.showSite = function(siteUrl) {
 this.siteUrl=siteUrl;
 return this.siteName+" The address is "+this.siteUrl;
};
var matou=new siteAdmin(" The home of the script ","WEB The front-end development ");
var matou2=new siteAdmin(" The home of the script ","WEB The front-end development ");
matou.age="30";
//  matou.showAdmin();
//  alert(matou.showSite("//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


Related articles: