JavaScript: isPrototypeOf and hasOwnProperty

Source: Internet
Author: User
Tags hasownproperty

1. isPrototypeOf

IsPrototypeOf is used to determine whether the specified object object1 exists in the prototype chain of another object object2. If yes, true is returned; otherwise, false is returned.

The format is as follows:

Object1.isPrototypeOf (object2 );

Object1 is an object instance;

Object2 is another object that will check its prototype chain.

The prototype chain can be used to share functions between different instances of the same object type.

If the prototype chain of object2 contains object1, The isPrototypeOf method returns true.

If object2 is not an object or object1 does not appear in the prototype chain of object2, The isPrototypeOf method returns false.

Example:

Var re =/^ \ s */; // a regular expression object is defined here // check whether RegExp is the original chain object of re and return truevar bIsptt = RegExp. prototype. isPrototypeOf (re );

2. hasOwnProperty

HasOwnProperty determines whether an object has a named property or object. This method cannot check whether the object has this property in the prototype chain. This property must be a member of the object.

If this attribute or method is defined by the object rather than defined in the prototype chain, true is returned; otherwise, false is returned;

The format is as follows:

Object. hasOwnProperty (proName );

Determine whether the name of proName is an attribute or object of the object. Example:

// Get false because the property var bStr = "Test String" in the prototype chain cannot be detected ". hasOwnProperty ("split"); // This property is already available on the prototype of the String object, and truevar bStr1 = String is naturally returned. prototype. hasOwnProperty ("split"); // return true because the property var bObj = ({fnTest: function () {}}) is not detected in the prototype (){}}). hasOwnProperty ("fnTest ");

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.