JS: In-depth prototype (top: Memory analysis)

Source: Internet
Author: User
Tags hasownproperty

/**
* The following shows how prototypes can be created using prototype-based creation to combine properties and methods
* Set to person exclusive, cannot be called through window.
* The prototype is a special object in JavaScript, and when a function is created, it produces a prototype object
* When a detailed object is created by the constructor of this function, in this detailed object, there will be a property pointing to the prototype
*/
The first state of
function person () {

}

A different state
Person.prototype.name = "Octopus";
Person.prototype.age = 23;
Person.prototype.say = function () {
Alert (this.name+ ":" +this.age);
}

The third kind of state
var p1 = new Person ();
P1.say (); Octopus:23
Say (); Say is not defined through window there is no way to call the Say method, so it is complete encapsulation

Detects if the P1 has a prototype _prop_ pointing to Person
Alert (Person.prototype.isPrototypeOf (p1)); True

Fourth state of
var p2 = new Person ();
P2.name = "Ada";
P2.say (); Ada:23

Detects if the P1 has a prototype _prop_ pointing to Person
Alert (Person.prototype.isPrototypeOf (p1)); True
Detect the constructor of an object
alert (P1.constructor==person); True
Detects if a property is its own property
Alert (P1.hasownproperty ("name")); False P1 has no value in its own space
Alert (P2.hasownproperty ("name")); True P2 set the name in its own space
Ability to delete properties in space via delete
Delete P2.name;
P2.say (); Octopus:23
Alert (P2.hasownproperty ("name")); False

Detects whether an object includes a property in the prototype or itself,
Alert ("name" in P1); True

Alert ("Address" in P1); False

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------

original articles such as reproduced, please indicate the source, this article started in CSDN site: http://blog.csdn.net/magneto7/article/details/24913525

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.