Differences between JavaScriptisPrototypeOf and hasOwnProperty _ js object-oriented

Source: Internet
Author: User
Tips for using criptisprototypeof and hasOwnProperty. If you need a friend, refer. 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:

The Code is as follows:


Var re =/^ \ s */;
// Define a regular expression object
// Check whether RegExp is the original link object of re. true is returned.
Var 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:

The Code is as follows:


// Get false because the attributes in the prototype chain cannot be detected.
Var bStr = "Test String". hasOwnProperty ("split ");
// This attribute is already available on the prototype of the String object. true is returned naturally.
Var bStr1 = String. prototype. hasOwnProperty ("split ");
// Returns true because it is not an attribute in the detection prototype.
Var bObj = ({fnTest: function () {}}). 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.