/**
* 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