Functions of the apply and call methods in JavaScript and Applications of prototype. js

Source: Internet
Author: User

The white point is actually to change the internal pointer of the object, that is, to change the content that this points to of the object. This is sometimes useful in Object-Oriented JS programming.

The first parameter of the call function and the apply method is the object to be passed into the current object, and this in the function. The following parameters are the parameters passed to the current object.

The apply and call functions are the same, but they have different parameters.
The meaning of the first parameter is the same, but for the second parameter:
The apply parameter is an array of parameters, that is, multiple parameters are combined into an array and the call parameter is passed in (starting with the second parameter ).
For example, the apply method for func. Call (func1, var1, var2, var3) is: func. Apply (func1, [var1, var2, var3]).

The advantage of using apply is that the arguments object of the current function can be passed in as the second parameter of apply.

(Reference: http://www.cnblogs.com/beyondnet/archive/2007/12/06/985216.html)

Test:

Function cls1 ()
{
This. B = '000000 ';
This. c = '20140901 ';
This. d = '000000 ';
This. f = function ()
{
Alert ("nihao ");
}
}

Function cls2 ()
{
This. A = 'aaa ';

Cls1.apply (this); // here we use classes (or functions) to apply them directly. Not an instance. In this case, the cls2 class has all the attributes and methods of the cls1 class.

}
VaR ST = new cls2 ();

Alert (St. D );

/* Note: The first class name can be used in the 2nd classes to replicate all attributes and methods. The method can be copied as follows: Ss. f. Apply (this); can run normally. The property ss. C. Apply (this); will report an error,

Function cls2 ()
{
This. A = 'aaa ';
VaR Ss = new cls1;
SS. C. Apply (this); // an error is reported here. If it is ss. f. Apply (this); then it is correct. If the attribute is used, the class name is used. If the method is used, that is, the available class name can also be used as an instance plus method (VAR Ss = new cls1; SS. f. apply (this );)

The format is as follows (*** correct ****).

}
VaR ST = new cls2 ();

Alert (St. C );

* ***** Correct *******
Function cls2 ()
{
This. A = 'aaa ';

Cls1.apply (this); // if it is an attribute, use the class name. If it is a method, you can use the class name. You can also add a method to the instance.

}

VaR ST = new cls2 ();

Alert (St. C );

_______________________________________________

Function simpleapplydemo (SIM ){
This. sim_name = SIM;
}
Function handlespa (hand ){
This. hand_name = hand;
Simpleapplydemo. Apply (this, new array ('20140901'); // The class name is used here.

//// Array () is used here to assign the initialization value of this array to the parameter in the simpleapplydemo function. That is, Sim = 222222222222. In the JS manual,Apply ([Thisobj[,Argarray])Argarra is an optional array of parameters that will be passed to the function. (That isArgarray is the same as arguments)

Alert (this. sim_name );
}

Handlespa ();

The above method achieves the same effect as the following method.

Function simpleapplydemo (SIM ){
This. sim_name = SIM;
}
Function handlespa (hand ){
This. hand_name = hand;
Simpleapplydemo. Apply (this, arguments); // The class name is used here.

/* Arguments is used here to upload arguments to simpleapplydemo () as a set of 55555555 parameters in handlespa ('20140901') as arguments (arguments stands for its own parameters, here only 5555555, that is, Sim = arguments = 55555 */

Alert (this. sim_name );
}

Handlespa ('20140901 ');

The call method in msdn calls a method of an object and replaces the current object with another object.

The apply method in msdn explains how to apply a method to an object and replace the current object with another object.

This explanation is very abstract. The two methods have the same role. For example

<SCRIPT>
Function cls1 ()
{
This. A = '000000 ';
}
Cls1.prototype. fun1 = function ()
{
Alert (this. );
}
Function cls2 ()
{
This. A = '000000 ';
}
VaR O1 = new cls1 ();
VaR O2 = new cls2 ();
O1.fun1. Apply (O2); // The instance name is used here. If it is written as o1.apply (O2), an error is returned.
</SCRIPT>

Only the class cls1 of the O1 object contains the fun1 method. However, in this case, we need to replace the O1 object with the O2 object, so this is displayed at this time. A will be 456. It's amazing. The call method is the same. The difference between the two methods is that the parameters are used differently. I will not explain it here.

You can see in prototype. js
VaR class = {
Create: function (){
Return function (){
This. Initialize. Apply (this, arguments );
}
}
}
ThisCodeA lot of people are easily confused by this bt code. In fact, it is not difficult to analyze the truth carefully.

Obviously, this statement indicates that class is an object declared. "CREATE" is an attribute of this object, and this attribute is a function. This function is returned after execution. The explanation may be too complicated, so it is better to do a test.

<SCRIPT>
VaR x = function () {return function () {alert (123 );}}
VaR n = x ();
N ();
</SCRIPT>
It's fun. Here N is the function that is returned after the X function is executed, that is, n is now equal to function () {alert (123);} And then executes N () and jumped out of 123.

This. Initialize. Apply (this, arguments );

What is the meaning of this sentence? Now let's take a look at how prototype. js can be called.

VaR template = Class. Create ();

Template. Prototype = {
Initialize: function (template, pattern ){
This. template = template. tostring ();
This. pattern = pattern | template. pattern;
},... Omitted code

VaR template = new template (replacement );

First, class. Create (); is returned to a function of the template. This function is
Function (){
This. Initialize. Apply (this, arguments );
}
When var template = new template (replacement); is executed, the function is executed.
Execute the initialize function in the current class.

Therefore, every class in prototype. JS is reserved.
Template. Prototype = {
Initialize: function (template, pattern ){
This. template = template. tostring ();
This. pattern = pattern | template. pattern;
},....
If this function is not available,ProgramAn error will occur.

Why do you want to write it like this?

In general, when we declare funciton fun () {} var o = new fun (); in this way, fun is a class and constructor, which is not a friendly code solution, so prototype. JS uses the above method

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.