JavaScript prototypes and inheritance descriptions _js object oriented

Source: Internet
Author: User
function A (x) 
{ 
this.x = x; 
} 
alert (a.prototype); 
alert (a.prototype.constructor);

According to the pop-up results, we can get: The prototype object is created by the constructor of the function, its properties can be shared by all objects, the primitive object points to an object, and a constructor property is defined, which points to the constructor itself that defines the prototype object. Then look at the following code.

Code

function A (x) 
{ 
a.prototype.x = x; 
} 
var obj = new A (A); 
alert (obj.x); 

Because all the properties of the prototype object are shared by the constructor, the object created can access the constructor property here, and obj corresponds to an instance created by the prototype object (prototype). So what happens when you rewrite the constructor property?

Code

function A (x) 
{ 
a.prototype.x = x; 
} 

var obja = new A (A); 
Obja.constructor = function (x) {alert ("overriding the constructor attribute of obj"); this.x =}; 
Obja.constructor (); 
alert (obja.x); 

var objb = new A (A); 
alert (objb.x);

As a result, we can see that the first pop-up is "rewrite the constructor attribute of obj" and then pop up 20, in the pop-up 10, visible, after we write rewrite Obja This object constructor, OBJB has not been changed, Therefore, no matter how many attributes are added or modified in an object, this does not affect the nature of the attributes in its prototype object, it is easy to understand why JS does this, because an object's behavior cannot affect other objects, otherwise it will cause confusion.

Here, we can sum up the rules of the code above:

1. When we call an object, we first check the object's own defined properties, and if so, call.

2. When a property of its own does not exist, the reference to the prototype object defined by its constructor is invoked.

So according to this rule, a prototype chain in JavaScript is formed, and we can define the inheritance relationship according to this rule.

function A (x) 
{ 
a.prototype.x = x; 
} 

function B (x,y) 
{ 
b.prototype.y = y; 
A.call (this,x); 
}

This code shows two functions, B function inheritance and a function, A.call (this.x), which passes the object of B to the a function for execution. Then, we also need the object constructed by B function to contain all the attributes of a function, so we need to add this sentence.

Copy Code code as follows:

B.prototype = new A ();
alert (b.prototype.constructor);

We first specify B's prototype as a, so the B function inherits the property of a function, and according to the pop-up result, we can see that its constructor is pointing to a function, so is the property of our B function lost? Therefore, we need to add a word, and finally give the continued integration code.

function A (x) 
{ 
a.prototype.x = x; 
A.prototype.showa = function () {alert ("Show Method!");} 

function B (x,y) 
{ 
b.prototype.y = y; 
A.call (this,x); 
B.PROTOTYPE.SHOWB = function () {alert ("Show method for B!");} 

B.prototype = new A (); 
B.prototype.constructor = B; 

var obj = new B (10,5); 
alert (obj.x); 
alert (OBJ.Y); 
Obj. ShowA (); 
Obj. SHOWB ();

If there is doubt or not, please comment and discuss.

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.