j53emw
Last Updated: February 25, 2016
·
637
· richardgong1987
Psb  1

javascript call(), apply() method

There are two additional methods for functions,that's apply(),call(). these methods both call the function with a specific this value, effectively setting the value of the this object inside the function body.

the apply() method method accept two arguments, the first arguments is scope, second argument may be an instance of Array ,things like the Array, arguments object,when we has to call an function but it's use local data to computer,here an example:

var funFirst = function(a, b){
        return a + b;
},funSecond = function(a, b){
        console.log(funFirst(a, b));
}(10, 20);//30

if we are need to pluse more number,then we don't know would us pass how much arguments in a function to calculate,ok,consider fellow example:

var funFirst = function(a, b){
        return a + b;
},funSecond = function(a, b){
        console.log(funFirst(a, b));
}(10, 20, 21, 22,23,24,25,26);//30

as above example ,funSecond didn't care my arguments ", 21, 22,23,24,25,26", it's directly ignored it;
does we need to "custom-made" function? things like:
var funFirst = function(a, b, c, d, e, f){
return a + b + c + d + e + f;
},funSecond = function(a, b, c, d, e, f){
console.log(funFirst(a, b, c, d, e, f));
}(10,10,10, 10, 10, 10);

if we pass in more.......what's would we do??
in this case,we can like this:

var funFirst = function(){
            var sum = 0;
            for(var i = 0; i < arguments.length; i++){
                    sum += arguments[i];
            }
        return sum;
},funSecond = function(){
        console.log(funFirst.apply(funSecond, arguments));
}(10, 20, 21, 22,23,24,25,26);//171 

in the funFirst.apply(funSecond, arguments)" code, funSecond is funFirst function run scope, pass in arguments array is "arguments" , (arguments like instance of Array, note: arguments object is not instance Array)

call(), apply() methods is very usefull;when we going to wrrite a javascript framework,or ,library;
call() almost same to apply(),but pass arguments type, using call() arguments must be enumerated specifically. ok, next. what's diff fellow example:

 _toString = function (obj) { 
     return Object.prototype.toString.call(obj); 
 }, 
 _toString2 = Object.prototype.toString,
 test = ["a", "b", "c"], 
console.log(_toString(test), _toString2(test),test.toString(),Object.toString(test))//[object Array] [object Undefined] a,b,c function Object() { [native code] } 

as you know, toString() method alway return [object objectname] at default, and you could cumstom it or rewrrite it; like this:

Object.prototype.toString = function(){
        return 'richardgong';
};
var a = Object(),
b = {};
alert(a)//richardgong
alert(b)//richardgong


var _toString = function (obj) { 
    return Array.prototype.toString.call(obj); 
}, 
_toString2 = Array.prototype.toString,
test = ["a", "b", "c"];

console.log(
_toString(test),
_toString2.call(test),
test.toString()
);//a,b,c a,b,c a,b,c 



slice = Array.prototype.slice;
void function(){
    var args = slice.call(arguments,2);
    console.log(args.concat(slice.call(arguments))) 
}('a','b','c')    //["c", "a", "b", "c"] 
Say Thanks
Respond