var a = Function.prototype.call.apply(function(a){return a;}, [0,4,3]);alert(a);这段代码怎么理解?Function.prototype.call ?

解决方案 »

  1.   

    先从这个函数说起var b = (function(a) {alert(this);//0
    return a;
    }).call(0, 4, 3);
    alert(b)
    弹出的是0与4
    也就是b再开始说
    var a = Function.prototype.call.apply(function(a){return a;}, [0,4,3]);alert(a);
    这个函数首先这个看
    var a = (Function.prototype.call).apply(function(a){return a;}, [0,4,3]); 
    把(Function.prototype.call)当成一个整体,call由浏览器实现的本地方法然后是这句
    fun.apply(obj,args)等价于obj.fun(args),这一步是重点,必须理解。
    (function(a){return a;}).apply(0,[4,3]) 
    (function(a){return a;}).call(0,4,3) 
    网上说的【重点必须理解】
    还不分析,让读者自己去理解,这作者只是提供了一个思路(function(a){return a;}).call(0,4,3)我觉得他这句有问题,让我开始非常迷惑,应该把0去掉我分析下第一次apply中0相当于那个obj参数是4,3
    然后实行apply上面那个call
    此时传递上来的参数是4,3
    所以call中的obj应该是4现在请看第一段代码那个b
    b等价于obj如果像第一段那样写就会弹出两次0与4连起来就是4
      

  2.   

    首先,把Function.prototype.call当成一个整体来看,它本身也是一个函数,因此和其他函数一样:具有apply方法。
    其次,
    call.apply(fun,[arg1,arg2,...])=fun.call(arg1,arg2,...)//apply()中的“数组”参数在call()中不再以数组形式存在,而是分别列了出来(本身call和apply的参数就是这样规定的:fun.call(this对象,传入fun的参数数组)-----fun.apply(this对象,传入fun的参数1,传入fun的参数2,...)。

    所以,按照这两个条件把上面的语句分解后可得到:
    var a=  function(a){return a;} .call (0,4,3);//fun.call()中的参数除第一个外,余下的为传入fun的参数。因此,在这里,4和3为传入function(a){return a}的参数,因为该匿名函数只需一个参数,因此按顺序只选到4做参数。个人理解,希望对你有帮助。
    可以自己做几个例子再测试验证一下。
      

  3.   

    我有点明白了。看了e文的ecma-262的文档 应该是像qwklove说的那样。附上:
    Function.prototype.apply (thisArg, argArray)
    When the apply method is called on an object func with arguments thisArg and argArray, the following steps are taken:
    1. If IsCallable(func) is false, then throw a TypeError exception.
    2. If argArray is null or undefined, then
    a. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and an empty list of arguments.
    3. If Type(argArray) is not Object, then throw a TypeError exception.
    4. Let len be the result of calling the [[Get]] internal method of argArray with argument "length".
    5. Let n be ToUint32(len).
    6. Let argList be an empty List.
    7. Let index be 0.
    8. Repeat while index < n
    a. Let indexName be ToString(index).
    b. Let nextArg be the result of calling the [[Get]] internal method of argArray with indexName as the argument.
    c. Append nextArg as the last element of argList.
    d. Set index to index + 1.
    9. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.
    The length property of the apply method is 2.
    NOTE The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, … ] ] )
    When the call method is called on an object func with argument thisArg and optional arguments arg1, arg2 etc, the following steps are taken:
    1. If IsCallable(func) is false, then throw a TypeError exception.
    2. Let argList be an empty List.
    3. If this method was called with more than one argument then in left to right order starting with arg1 append each argument as the last element of argList
    4. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.
    The length property of the call method is 1.
    NOTE The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.