JavaScript Learning Note 09 (constructors, prototypes,)

Source: Internet
Author: User

Prototype:

<script type= "Text/javascript" >
/**
* The following shows how prototypes are created, using prototype-based creation to set properties and methods
* is set to person-specific and can no longer be called by window
*/
function person () {

}
Person.prototype.name = "Leon";
Person.prototype.age = 23;
Person.prototype.say = function () {
Alert (this.name+ "," +this.age);
}
var p1 = new Person ();
P1.say ();
There is no way to invoke the say method through window, so the package is completed
Say ();
</script>

Four states of the prototype:

<script type= "Text/javascript" >
/**
* The prototype is a very special object in JS, and when a function is created, it produces a prototype object.
* When a specific object is created by the constructor of this function, in this specific object
* There will be a property pointing to the prototype
*/
The first state of
function person () {

}
Second State of
Person.prototype.name = "Leon";
Person.prototype.age = 23;
Person.prototype.say = function () {
Alert (this.name+ "," +this.age);
}
The third State, after an object has been created, will have a _prop_ attribute pointing to the prototype
If the property is not found inside the object when it is used, it will be found in the prototype, the _prop_ property is hidden
var p1 = new Person ();
P1.say ();
The following methods can be used to detect if the P1 has a _prop_ point to person's prototype
Alert (Person.prototype.isPrototypeOf (p1));

Fourth state of
var p2 = new Person ();
is to define an attribute in its own space that does not replace the property in the prototype
P2.name = "Ada";
P2.say ();
P1.say ();

Detects whether an object is a prototype of a function
Alert (Person.prototype.isPrototypeOf (p2));

Detecting the constructor of an object
alert (P1.constructor==person);

To detect whether 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 property in its own space

Delete P2.name;
P2.say ();
Alert (P2.hasownproperty ("name"));//Because it has been deleted, it is false

Detects whether an object contains a property in the prototype or itself, through in detection
Alert ("name" in p1);//true
Alert ("name" in P2);//true
Alert ("Address" in p1);//Not in prototype and in own space, false

Alert (Hasprototypeproperty (P1, "name"));//true
Alert (Hasprototypeproperty (P2, "name"));//false
/**
* You can detect whether a property exists in the prototype by the following methods
*/
function Hasprototypeproperty (obj,prop) {
Return ((!obj.hasownproperty (prop) && (prop in obj))
}
</script>

JavaScript Learning Note 09 (constructors, prototypes,)

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.