www.webdeveloper.com
Results 1 to 3 of 3

Thread: how to figure it out?

  1. #1
    Join Date
    Apr 2011
    Posts
    7

    how to figure it out?

    var a = Function.prototype.call.apply(function(a){return a;}, [0,4,3]);alert(a);

    i know the answer is 4, but dont know how ?

  2. #2
    Join Date
    Apr 2011
    Posts
    7
    i found something in ecma-262 docs . i want to know whether it is the way is .

    var a=Function.prototype.call.apply(function(a) {return a;},[0,4,3]) =>

    var a=(function(a) {return a;}).call(0,4,3) =>
    var a=4


    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.

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    First ligth...
    Code:
    var a = Function.prototype.call.apply(function(a){
        var i,str='This value:'+this;
        for (i in arguments) str+='\nkey:'+i+'-value:'+arguments[i];
        alert(str)
        return a;}, [0,4,3]);alert(a);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles