Call, apply && bind, currying

Source: Internet
Author: User
Tags array object bind functions return tostring

Brief summary:


Apply (): Call the function as a method of the specified object, passed to it is the specified argument array
--function.apply (thisobj, args) or function.apply (thisobj, args)

Bind (): main function: Bind a function to an object, return a new function, call the method with the specified object's method by optional specified arguments
--function.bind (o) or Function.bind (O, args ...);

Call (): Similar to apply, calls the function as a method of the specified object, passing it the specified argument
--function.call (thisobj, args ...)




Apply



Function.apply (thisobj, args)

1. Thisobj is the object that invokes the function, which is thisobj to this, and uses the global object if the argument is null

2, returns the return value of the calling function

eg


var data = [1, 2, 3, 4, 5];
Math.max.apply (null, data); 5


var data = [1, 2, 3, 4, 5];
Console.log (Data.tostring ()); 1,2,3,4,5
Apply the default ToString method to an object
Object.prototype.toString.apply (data); "[Object Array]"




Call



Function.call (thisobj, args ...) --Similar to apply usage, except that the parameter passed is not an array

1. Thisobj is the object that invokes the function, which is thisobj to this, and uses the global object if the argument is null

2, returns the return value of the calling function

eg


Call
var data = [1, 2, 3, 4, 5];
Math.max.call (null, data); NAN
Math.max.call (NULL, 1, 2, 3, 4, 5); 5

var data = [1, 2, 3, 4, 5];
Object.prototype.toString.call (data); "[Object Array]"
Object.prototype.toString.call (1); "[Object number]"




Bind



Function.bind (o, args)

1, O to bind to the object on the function

2, args ... 0 or more function values to bind to on a function

3. Returns a new function that is invoked as an O method and passes it to the args parameter



EG1:


Bind
var sum = function (y) {
return this.x + y;
}
var obj = {X:1};
var objsum = sum.bind (obj);
Objsum (3); 4





EG2:


function sum () {
var result=0;
For (Var i=0, n=arguments.length iresult = Arguments[i];
}
return result;
}

var newsum = Sum.bind (null, 1); Here this value is bound to null, and the first parameter is bound to 1
Newsum (2); 3
Newsum (2, 3, 4); 10





EG3:


function sum () {
var result=0;
For (Var i=0, n=arguments.length iresult = Arguments[i];
}
return result;
}

var newsum = Sum.bind (null, 1, 2); Here this value is bound to null, and the first and second parameters are bound to 1, 2
Newsum (2); 5
Newsum (2, 3, 4); 12





Examples 2 and 3 contain the idea of function currying, which combines the default and variable parameters of a function, transforms a function that accepts multiple parameters into a function that accepts a single parameter, and returns a new function that accepts the parameters of the original function.

The main function of currying is to create functions dynamically through existing functions, and create new functions that still function through existing functions, simply by passing in more parameters to simplify the invocation of function parameters.

In addition, Cory uses a lot of the DOM's callback function



EG1:


var curry = function (fn) {
var args = Array.prototype.slice.call (arguments, 1);
return function () {
return fn.apply (NULL, Args.concat (Array.prototype.slice.call (arguments)));
}
}
function sum () {
var result=0;
For (Var i=0, n=arguments.length iresult = Arguments[i];
}
return result;
}

var newsum= curry (sum, 1, 2);
Newsum= newsum (1, 2, 3); 9





EG2:


var curry = function (fn) {
var args=[];
return function () {
if (arguments.length) {
[].push.apply (Args,arguments);
return Arguments.callee;
}else{
Return fn.apply (This,args);
}
}
};

function sum () {
var result=0;
For (Var i=0, n=arguments.length iresult = Arguments[i];
}
return result;
}

var newsum= curry (sum);
newsum= newsum (1) (2) (3);
Newsum (); 6
Newsum (4) (); 10





Learn more currying can refer to link 1, link two



In addition, bind is a new method in ECMAScript5 and can be used in ECMASCRIPT3 as follows:


Bind is a new method in ECMAScript5 and can be used in ECMASCRIPT3 as follows:

function bind (f, O) {
if (f.bind) {
return F.bind (o);
}else{
return function () {
Return f.apply (o, arguments);
}
}
}



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.