Go The JavaScript hasOwnProperty () function is detailed

Source: Internet
Author: User

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

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.