hasOwnProperty()The function is used to indicate whether an object itself (excluding the prototype chain) has a property with the specified name. If there is, return true , otherwise return false .
This method belongs Object to an object, and since all objects have "inherited" an object instance, this method can be used by almost all instance objects.
This function is supported by mainstream browsers such as IE 5.5+, FireFox, Chrome, Safari, and opera.
Grammar
Object. hasOwnProperty()
Parameters
| Parameters |
Description |
| PropertyName |
Property name specified by string type |
return value
hasOwnProperty()The return value of the function is of type Boolean. If object the object has a property named, propertyName it is returned true , otherwise returned false .
This method does not check for the existence of the property in the object's prototype chain, which is returned only by a member of the object itself true .
Example & Description
function Site(){
This.Name= "Codeplayer";
This.Url= "http://www.365mini.com/";
This.SayHello= function(){
Document.Writeln("Welcome here." + This.Name);
};
}
VarObj= {
Engine: "PHP"
,Sayhi: function(){
Document.Writeln("Welcome to Visit" + This.Url);
}
};
Overwrite the prototype property of the site itself with object obj
Site.Prototype=Obj;
VarS=New Site();
Document.Writeln(S.hasOwnProperty("Name") ); True
Document.Writeln(S.hasOwnProperty("SayHello") ); True
The following properties are inherited from the prototype chain and therefore are false
Document.Writeln(S.hasOwnProperty("Engine") ); False
Document.Writeln(S.hasOwnProperty("Sayhi") ); False
Document.Writeln(S.hasOwnProperty("ToString") ); False
//want to see if the object (including the prototype chain) has a specified property, you can use the in operator
document< Span class= "pun". writeln ( "engine" in S Span class= "pun"); //true
Document. "Sayhi" in S ); //true
Document. ( "toString" in S //true
(GO) JavaScript hasownproperty () function in detail