JS Zhongyuan type, learning and understanding of prototype chain

Source: Internet
Author: User

Meaning of the prototype: if the constructor has a prototype object a, the instances created by the constructor must be copied to a. A prototype is also an object that can be acquired and changed at run time. You can add attributes to the prototype or delete properties that already exist on the prototype . The prototype object is used to implement inheritance.

The 2.prototype attribute, which is the property of each function, (all types of constructors are also functions), prototype is a property containing the constructor attribute. object, where the constructor property holds A reference to the function.

the properties on the 3.prototype can be shared by all instances because each instance has a _proto_ property.

4 prototype chain: all objects in Js ( except Undefined,null) have a built -in [[prototype]] (_proto_) attribute pointing to its "parent class" prototype. put this there . _proto_ strung up until the Object.prototype.proto to be NULL chain is called the prototype chain. .

5.function Fun () {

Code

}

The following actions are performed inside this function:

Add a prototype (or prototype) property to the function, adding an additional constructor property to the prototype object . And this property holds a reference to the function Fun () .

var obj = new Fun (); when you use a constructor to create an instance object, an instance of the object automatically saves a pointer to its constructor. prototype an attribute of the object _proto_, so in each of our object instances, we can access the constructor's prototype all the properties and methods.  

The following example illustrates the role of prototypes and the embodiment of the prototype chain. :

function createperson (name,age,job) { this.name = name;  this.age = age; this.job = job; } createperson.prototype.firstname  =  "XI";  createperson.prototype.getname = function ()  { alert (this.name);  }; var per1 = new createperson ("Xixi", "SE");   var per2 =  new createperson ("Xiyin", "F", "FF"); 

Createperson . Prototype sets the properties of a function object firstname, and a method GetName (), that Createperson The normal objects that come out of an instance (in the example:per1,per2) inherit these properties and methods, which is the role of prototypes.

Use a diagram to illustrate the whole process, as shown below:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/80/2C/wKioL1c6jtmAGsQMAAF_4pYfGiM784.jpg "title=" 1.jpg " alt= "Wkiol1c6jtmagsqmaaf_4pyfgim784.jpg"/>


This article is from the "dream need to adhere to" blog, please be sure to keep this source http://xiyin001.blog.51cto.com/9831864/1774258

JS Zhongyuan type, learning and understanding of prototype chain

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.