The role and difference of call and apply

Source: Internet
Author: User

Call and apply are used to invoke the function and replace the function's this value with the specified object (the first argument), replacing the function's arguments with the specified array. Note: You can also not specify parameters, this is simply called functions, such as: Fun.call ()

Grammar:

Fun.call (THISOBJ,ARG1,ARG2); with call when the number of arguments is determined, the arguments are concatenated with commas

Fun.apply (Thisobj,[arg1,arg2]), with apply when the subsequent parameter number is indeterminate, the argument is entered as an array

The role of call and apply:

To change the direction of this, the first parameter is the object you want to pass in, and this is the object that is passed to the following function, which is the parameter value you pass for the function.

Simple case:

var str= ' JS ';
function fo () {
var str= ' JQ ';
Console.log (THIS.STR);//This point points to window
Console.log (This===window);
Console.log (This===obj);
Output Js,true,false
}
Fo ();
var obj={
STR: ' HTML '
};
Fo.call (obj)//Output html,false,true, which proves that call changed the this point of fo to obj;
var ob={
STR: ' CSS ',
}
var get=function () {
Console.log (str);//js reads the value of the global variable at this time
Console.log (THIS.STR);//css this point to the OB, the return is OB.STR
}
Get.call (OB)

Complex case:

function log () {
var args=array.prototype.slice.call (arguments);//convert parameter to array

The Slice (start,end (not required)) method returns the selected element from an existing array.

The parameter object in the JavaScript function arguments is an object, not an array. But it can access the elements in a table like an array, and it also has the number of elements that the length property identifies. Usually we convert it to an array with the slice function, the sample code is as follows: function fn () {var arr = Array.prototype.slice.call (arguments,0);}

So Array.prototype.slice.call (arguments) is to convert the argument to an array and return the array

Console.log (args);//array ["Hello", "World"]
Args.unshift (' (gykj) ');//unshift (a,b,c) adds at least one element to the beginning of the array
Console.log (args);//array ["(GYKJ)", "Hello", "World"]
Console.log.apply (Console,args);//(GYKJ) Hello World apply can output an array element as a character parameter

Because the number of arguments is not sure, you can only use apply
}
Log (' Hello ', ' world ')

The role and difference of call and apply

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.