The apply,call of JavaScript technical difficulties and the interface between this and this _javascript skills

Source: Internet
Author: User

1.apply definition

Apply: Call the function and replace the value of the function with the specified object, replacing the parameters of the function with the specified array.

Syntax: Apply ([Thisobj[,argarray]])

Thisobj

Optional. The object to use as the This object.

Argarray

Optional. A set of parameters to pass to the function.

2.call definition

Call: Invokes the method of an object, replacing the current object with another object.

Syntax: Call ([thisobj[, arg1[, arg2[, [, Argn]]]]

Thisobj

Optional. The object that will be used as the current object.

Arg1, Arg2, argn.

Optional. The argument list that will be passed to the method.

3. The difference between the two

The second argument to call can be any type, and the second argument to apply must be an array or a arguments.

There are also differences in definitions.

4. Example Analysis

(1) Official examples:

 function callMe (arg1, arg2) {var s = "";
  S + + ' This value: ' + this;
  S + + "<br/>";
    For (i in callme.arguments) {s + = "arguments:" + callme.arguments[i];
  S + + "<br/>";
return s;
} document.write ("Original function: <br/>");
document.write (CallMe (1, 2));
document.write ("<br/>");
document.write ("Function called with apply: <br/>");
document.write (Callme.apply (3, [4, 5]));
document.write (Callme.call (3, 4, 5)); Output://Original function://This value: [Object Window]//arguments:1//arguments:2/function called with AP Ply://This value:3//arguments:4//Arguments:5 

The first applied: definition: Calling a function, and substituting the this value of the specified object for the function CallMe, replaces this in the CallMe function with the specified object 3, where this is changed from the previous [object Window] to 3. The first call: Definition: A method that invokes an object and replaces the current object with another object. Invokes the object CallMe method, replacing the object in CallMe with another object, 3. As can be seen from the analysis of these results, both use the object in the specified object or the specified value to change this in the object. It can also be said that an object in a function (this) "hijacked" another object (this) to perform in the function. In fact, this raises a question: what is this? Why is it so important to change the direction of the mind again and again? Portal: JavaScript Technical Difficulties (iii) this, new, apply and call details (this is great, absolutely enough for you to drink) (2) Example:

function Zqz (a,b) {return
  alert (a+b);
}
function Zqz_1 (a,b) {
  zqz.apply (zqz_1,[a,b])
}
zqz_1 (1,2)  

Profiling: Calling a function by definition and replacing the this value of the function with the specified object,

This calls the function ZQZ and replaces the This value of the ZQZ function with the specified object zqz_1.

Note that the function name in JS is actually an object, because the function name is a reference to the Funtion object!

function Add (A, B)
{
 alert (a + B);
}
function Sub (A, b)
{
 alert (a-b);
}

Profiling: By definition: Invokes an object's method, replacing the current object with another object.

This is the method that invokes the object add and replaces the current object sub with the Add object;

One more example:

function Zqz (a,b) {
  this.name=a;
  this.age=b;
  Alert (this.name+ "" +this.age);
}
function Zqz_1 (a,b) {
  zqz.apply (this,[a,b])   //We can also write  zqz.apply (this,arguments) 
}
zqz_1 (" Nic ", 12)  

Profiling: Calling a function by definition and replacing the this value of the function with the specified object,

This invokes the function ZQZ, using the specified object, this replaces the function ZQZ.

One more example:

<input type= "text" id= "MyText"  value= "input text" >
function Obj () {
  this.value= Object! ";
}
var value= "global variable";
function Fun1 () {
  alert (this.value);
}
Fun1 ();  Global variable
fun1.call (window);//global variable
fun1.call (document.getElementById (' MyText '));//input text
Fun1.call (New OBJ ());  Object!

Analysis: Definition: Invokes the method of an object, replacing the current object with another object.

Invokes the method of the Fun1 object, replacing the object in the current Fun1 with a Window object.

Invokes the method of the Fun1 object, replacing the object in the current Fun1 with the object in input.

Invokes the method of the Fun1 object, replacing the object in the current Fun1 with an object in the newly created obj.

Finally, let's summarize:

The this pointer to a function points to window if it is not in the JavaScript language through new (including object literal definition), call, and apply to change the this pointer of the function.

Other ingenious uses of ps:apply:

Do you think that since apply is similar to call, why do you still have both of these methods? You can lose one of them altogether. It was later discovered that an article about apply because it has many other magical uses for its parameters.

A) Math.max can achieve the largest of the array:

Because the Math.max parameter does not support Math.max ([PARAM1,PARAM2]), which is an array, but it supports Math.max (Param1,param2,param3 ...), you can resolve Var max= according to the features of the Apply Math.max.apply (Null,array), so that you can easily get one of the largest in an array. (Apply will convert an array to a parameter passed to a method)

The first parameter in the call gives a null, because there is no object to invoke the method, just use this method to help the operation, get the returned results on the line, so directly passing a null past.

b) Math.min can achieve the smallest of the array:

Also and Max is an idea of Var min=math.min.apply (Null,array).

c) Array.prototype.push can implement two array merges:

The same push method does not provide an array of push, but it provides a push (Param1,param,... paramn) So you can also convert the array by using apply:

var arr1=new Array ("1", "2", "3");
var arr2=new Array ("4", "5", "6");

It can also be understood that arr1 invokes the push method, where the parameter is assembled by apply to the collection of parameter lists.

d) Summary: Under what circumstances, special uses such as math.min can be used:

In general, the target function requires only n parameter lists without receiving the form of an array ([param1[,param2[,... [, Paramn]]]] ), you can solve this problem skillfully through the Apply method.

Related Article

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.