JS's Prototype property

Source: Internet
Author: User
Tags hasownproperty

Prototype action on an object in JS, you can add properties and methods to an object.

Syntax: Object.prototype.name=value

Details: When an object is created through a class, the order in which the JS looks for properties is:

1. Properties of the object itself, such as obj={} obj.age=18

2. Find prototype in the class of the object, such as obj={} obj.prototype.age=18

3. Find in the object's root object (object)

4. After the class definition of an object, you can add a new property and method to the class of the object through prototype, after which the property or method is available through the newly created object of the class

function Persion () {
This.name= "Xiaol"
}

P1 = new Persion ()

P1.name//"Xiaol"


Persion.prototype.age = 18


P1.age//18


P2 = new Persion ()

P2.age//18

5. Obj.hasownproperty (attrname) to determine whether a property is an attribute of the object itself, such as Obj.hasownproperty ("age") returns false

6. Use Attrname in obj to determine whether a property is within an object (including its own and prototype properties), for example: "Age" in P1 returns ture

JS's Prototype property

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.