JavaScript isprototypeof and hasOwnProperty use difference _js object oriented

Source: Internet
Author: User
Tags regular expression hasownproperty
1, isprototypeof
isPrototypeOf is used to determine whether the specified object Object1 exists in a prototype chain of another object Object2, returns True, or false.
The format is as follows:
Object1.isprototypeof (OBJECT2);
Object1 is an example of an object;
Object2 is another object that will check its prototype chain.
A prototype chain can be used to share functionality between different instances of the same object type.
If the OBJECT2 's prototype chain contains Object1, then the isPrototypeOf method returns True.
If Object2 is not an object or Object1 does not appear in the prototype chain in Object2, the isPrototypeOf method returns false.
Examples of use are as follows:
Copy Code code as follows:

var re =/^\s*/;
This defines a regular expression object
Here check regexp is not the prototype chain object of RE, return True
var bisptt = RegExp.prototype.isPrototypeOf (re);

2, hasOwnProperty
hasOwnProperty a property or object that determines whether an object has a name or not, this method cannot check whether the object's prototype chain has the attribute, which must be a member of the object itself.
Returns true if the property or method is defined by the object itself, rather than defined in the prototype chain;
The format is as follows:
Object.hasownproperty (Proname);
Determines whether the name of the Proname is a property or object of the object. Examples of use are as follows:
Copy Code code as follows:

Get false because the properties in the prototype chain cannot be detected
var bStr = "Test String". hasOwnProperty ("split");
This property is inherently on the prototype of a string object and returns True Naturally
var bStr1 = String.prototype.hasOwnProperty ("split");
Returns true because the properties in the prototype are not detected
var bobj = ({fntest:function () {}}). hasOwnProperty ("Fntest");

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.