How to call () and apply () based on the Inheritance Mechanism in JavaScript

Source: Internet
Author: User

Call () method

The call () method is the most similar to the classic object impersonating method. Its first parameter is used as the object of this. Other parameters are directly passed to the function itself.For example:
Copy codeThe Code is as follows:
Function sayHello (sPrefix, sSuffix ){
Alert (this. name + "says" + sPrefix + sSuffix );
};

Var obj = new Object ();
Obj. name = "Tom ";

SayHello. call (obj, "Hello", "World .");

In this example, the function sayHello () is defined outside the object, and the keyword this can be referenced even if it does not belong to any object. The name attribute of object obj is equal to blue. When calling the call () method, the first parameter is obj, which indicates that the value of this keyword in the sayHello () function should be given to obj. The second and third parameters are strings. They match the sPrefix and sSuffix parameters in the sayHello () function, and the last generated message "Tom says Hello World." will be displayed.

To use this method together with the object impersonating method of the Inheritance Mechanism, you only need to replace the values, calls, and deletion codes of the first three rows:
Copy codeThe Code is as follows:
Function ClassA (sColor ){
This. color = sColor;
This. sayColor = function (){
Alert (this. color );
};
}


Function ClassB (sColor, sName ){
// This. newMethod = ClassA;
// This. newMethod (color );
// Delete this. newMethod;
ClassA. call (this, sColor );

This. name = sName;
This. sayName = function (){
Alert (this. name );
};
}

Here, we need to make the keyword this in ClassA equal to the newly created ClassB object, so this is the first parameter. The second parameter sColor is a unique parameter for both classes.

Apply () method

The apply () method has two parameters: the object used as this and the array of parameters to be passed to the function.For example:
Copy codeThe Code is 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 ."));

This example is the same as the previous example, but now the apply () method is called. When the apply () method is called, the first parameter is still obj, which indicates that the value of this keyword in the sayColor () function should be given to obj. The second parameter is an array composed of two strings. It matches The sPrefix and sSuffix parameters in The sayColor () function. The final message is still "The color is blue, a very nice color indeed. ", will be displayed.

This method is also used to replace the code for assigning values to, calling, and deleting new methods in the first three rows:
Copy codeThe Code is as follows:
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 );
};
}

Similarly, the first parameter is still this, and the second parameter is an array with only one value of color. You can pass the entire arguments object of ClassB to the apply () method as the second parameter:
Copy codeThe Code is as follows:
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 );
};
}

Of course, parameter objects can be passed only when the Parameter order in the superclass is exactly the same as that in the subclass. If not, you must create a separate array and place the parameters in the correct order. You can also use the call () method.

We can see that these two methods can replace the original object impersonating, making the writing a little simpler. However, the disadvantage of these methods is that the subclass cannot inherit the methods or attributes declared by the parent class on the prototype chain, the next article will introduce another method of implementation inheritance in JavaScript-prototype chain inheritance.

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.