JavaScript Advanced Programming Learning Notes (ii)

Source: Internet
Author: User

The benefit of writing a reading note is to deepen the memory, which summarizes several ways in which objects are created in programming, as well as commonly used methods, which summarize how inheritance is implemented:
1. objects posing as:

function ClassA (scolor) {

This.color = Scolor;this.saycolor = function () {    alert (this.color);};

}

function ClassB (Scolor, SName) {

This.newmethod = Classa;this.newmethod (scolor);d elete this.newmethod;this.name = Sname;this.sayname = function () {    alert (this.name);};

}

var obja = new ClassA ("Blue");

var objb = new ClassB ("Red", "John");

Obja.saycolor (); Output "Blue"

Objb.saycolor (); Output "Red"

Objb.sayname (); Output "John"

Object impersonation can implement multiple inheritance:

function Classz () {

This.newmethod = Classx;this.newmethod ();d elete This.newmethod;this.newmethod = Classy;this.newmethod ();d elete This.newmethod;

}

The third edition of ECMAScript adds two methods to the Function object, call () and apply ()

2. use the call () method (the method most similar to the Classic object impersonation method)

function Saycolor (sprefix,ssuffix) {

Alert (Sprefix + **this.color** + ssuffix);

};

var obj = new Object ();

Obj.color = "Blue";

Saycolor.call (obj, "The color is", "a very nice color indeed.");

The This.color in the inside is obj.

function ClassB (Scolor, SName) {

This.newmethod = Classa;//this.newmethod (color);//delete This.newmethod; Classa.call (this, scolor); Executes the This.color and This.saycolor statements inside the object ClassA, and this represents the incoming CLASSB, which adds two members to CLASSB = THIS.name = function () {    alert (this.name);};

}

3. use the Apply () method

The Apply method is similar to the call method,

The Apply method is used as follows:

function Saycolor (sprefix,ssuffix) {

Alert (Sprefix + This.color + ssuffix);

};

var obj = new Object ();

Obj.color = "Blue";

Saycolor.apply (obj, New Array ("The Color is", "a very nice color indeed."));

When creating CLASSB, you can use:

(1)
function ClassB (Scolor, SName) {

This.newmethod = Classa;//this.newmethod (color);//delete This.newmethod; Classa.apply (This, new Array (Scolor)), this.name = Sname;this.sayname = function () {    alert (this.name);};

}

(2)
function ClassB (Scolor, SName) {

This.newmethod = Classa;//this.newmethod (color);//delete This.newmethod; Classa.apply (this, arguments); this.name = Sname;this.sayname = function () {    alert (this.name);};

}

You can see that only the parameters of the object are different, the array used or the arguments

4. Use the prototype chain

(1)
function ClassA () {

}

ClassA.prototype.color = "Blue";

ClassA.prototype.sayColor = function () {

alert (This.color);

};

function ClassB () {

}

Classb.prototype = new ClassA ();

(2)

function ClassB () {

}

Classb.prototype = new ClassA ();

If the above sentence code (Classb.prototype = new ClassA (); ), after the following two lines of code, the added Name,sayname members will be lost

ClassB.prototype.name = "";

ClassB.prototype.sayName = function () {

alert (this.name);

};

So be aware of the sequence when using this method

You can also use instanceof when using the prototype chain:
var objb = new ClassB ();

Alert (OBJB instanceof ClassA); Output "true"

Alert (OBJB instanceof ClassB); Output "true"

But the disadvantage of using a prototype chain is that multiple inheritance is not supported, and the prototype chain overrides the class's prototype property with another type of object.

5. Mixed Mode

Constructor methods must be used if the object is impersonating, so it is not the best choice, but the constructor with parameters cannot be used with the prototype chain.
, such as using the (2) code inside the prototype chain, ClassA if it is a constructor, then the object must be created first.
Classb.prototype = new ClassA (XXX);
Re-execution:
ClassB.prototype.name = "";

ClassB.prototype.sayName = function () {

alert (this.name);

};

This is unrealistic, because you first defined the CLASSB prototype method, use the time to create the instance directly
, so that no parameters can be passed into the ClassA, so the final blend is used (below)

function ClassA (scolor) {

This.color = Scolor;

}

ClassA.prototype.sayColor = function () {

alert (This.color);

};

function ClassB (Scolor, SName) {

Classa.call (this, scolor); this.name = SName;

}

Classb.prototype = new ClassA ();

ClassB.prototype.sayName = function () {

alert (this.name);

};

When you use this method, you can use the parameters passed in to ClassA by creating CLASSB.

Learning Summary:

So the most common way to implement inheritance is in a hybrid way, and there is a prototype chain that pretends to be a way.

JavaScript Advanced Programming Learning Notes (ii)

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.