JavaScript checks instance attributes and prototype attributes _ javascript skills

Source: Internet
Author: User
This article mainly introduces the JavaScript detection of instance attributes and prototype attributes. For more information about the attributes, see <g id = "1"> prerequisite </g>.

The property of a JavaScript object can be divided into two types: exist in an instance and exist in a prototype object.

According to the above, four situations may occur during attribute detection.

Neither instance nor prototype object
Exist in instance, not in prototype object
The instance does not exist and the prototype object exists.
Both instances and prototype objects exist.

1. hasOwnPrototype ()

HasOwnPrototype () accepts an attribute name in string format. if the instance itself has this attribute (case 2/case 4), true is returned. otherwise, false is returned (case 1/case 3 ).

The code is as follows:


Functino Person (){}
Person. prototype. name = 'apple ';
Var person1 = new Person ();
Var person2 = new Person ();
Person1.name = 'banana ';
Console. log (person1.hasOwnPrototype (name); // true
Console. log (person2.hasOwnPrototype (name); // false

2. in operator

The in operator returns true (case 2/case 3/case 4) regardless of whether the attribute exists in the instance or the prototype object. otherwise, false (case 1) is returned ).

The code is as follows:


Console. log ('name' in person1); // true
Console. log ('name' in person2); // true

3. check the prototype attributes.

Combined with the in operator and hasOwnProperty (), you can define a function to check whether a given attribute exists in the prototype.

The code is as follows:


Function hasPrototypeProperty (object, name ){
Return! Object. hasOwnPrototype (name) & (name in object );
}
Console. log (hasPrototypeProperty (person1, 'name'); // false
Console. log (hasPrototypeProperty (person2, 'name'); // true

If a given attribute exists in the prototype, true is returned (case 3). otherwise, false is returned (case 1/case 2/case 4 ).

The above is all the content of this article. I hope you will like it.

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.