what is Array.map(Function.call,Number)

前端 未结 4 1994
北荒
北荒 2021-02-06 01:36
var array1 = [1, 4, 9, 16];

map1=array1.map(Function.call,Number);

Why the output of map1 is [0,1,2,3], what this map function is doing?

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 01:49

    Array.map function creates a new Array, with the results of calling the provided Function (Function.call) on every element in the Array that calls Array.map.

    e.g.

    // Store an initial Array of String values
    var stringArray = ['Matt','Long','JavaScript'];
    
    // Invoke Function for each value of Array
     mapResult = stringArray.map((currentValue) => {
         // Concatenate and return
        return currentValue + ' Software';
    });
    
    // Log map result
    console.log(mapResult);
    

    You are passing in Number as the 'this' value to use, when executing callback for each value in the Array. In the result Array, each element is the result of calling the given Function on each value in the Array.

    The Function.call method calls a Function with a given 'this' value and the arguments provided.

提交回复
热议问题