为什么我们在 `map()` 函数中使用 `Number.prototype.valueOf`

Why do we use `Number.prototype.valueOf` inside of a `map()` function

以下代码:

let resultsArray = Array.apply(null, Array(10)).map(Number.prototype.valueOf,0);

创建以下数组

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

为什么 map() 需要 Number.prototype.valueOf 只是将数字 0 推入此数组的每个位置。是否有不同的(更有效的)方法来实现这个结果,或者这是最好的方法?

在您的代码中,0 被传递给 Number.prototype.valueOf,后者只是 returns 原语 (0)。

这与

本质上是一样的
Number.prototype.valueOf.call(0)

如果您的代码是:

let resultsArray = Array.apply(null, Array(10)).map(Number.prototype.valueOf,10);

map() 函数本质上是这样调用 Number.prototype.valueOf

Number.prototype.valueOf.call(10)

这是 documentation - 它提供了一个很好的 Polyfill,您可以阅读它并确切地看到 map() 在做什么。


字符串看起来像这样:

Array.apply(null, Array(10)).map(String.prototype.valueOf,"10")

这将输出:

["10", "10", "10", "10", "10", "10", "10", "10", "10", "10"]

但你当然可以用更棘手的方式来做 ;)

var resultsArray = '0'.repeat(10).split('').map(Number);
console.log(resultsArray);

如果不需要获取数字(但字符串),它甚至更短:

var resultsArray = '0'.repeat(10).split('');
console.log(resultsArray);

还有一个解决方案

Array.from(new Array(10), () => 0)

广告如果你的浏览器不支持ES6,那么

Array.from(new Array(10), function(){return 0;})

如果你阅读 map documentation 你可以阅读这个:

The map() method creates a new array with the results of calling a provided function on every element in this array.

因此您需要在第一个参数中使用函数 Number.prototype.valueOf 并在第二个可选参数中使用 Number 对象来填充新数组。 Number 对象用作第一个参数的 this。

你也可以这样写得到同样的结果:

let resultsArray = Array.apply(null, Array(10)).map(function(){return 0;});

但是如果你只想填充一个数组,我想你可以使用Array.prototype.fill方法。

The fill() method fills all the elements of an array from a start index to an end index with a static value.

let resultsArray = (new Array(10)).fill(0);

性能测试:

var start, stop, array;
var iteration = 100000;


// Map
start = Date.now();
array = Array.apply(null, Array(iteration)).map(Number.prototype.valueOf,0);
stop = Date.now();
console.log("Map executed in "+(stop-start)+"ms");

// Map simple array
start = Date.now();
array = (new Array(iteration)).map(Number.prototype.valueOf,0);
stop = Date.now();
console.log("Map simple array executed in "+(stop-start)+"ms");

// Map simple function
start = Date.now();
array = (new Array(iteration)).map(function(){return 0;});
stop = Date.now();
console.log("Map simple function executed in "+(stop-start)+"ms");

// Array.from - ES6 from @Zohaib ijaz
start = Date.now();
array = Array.from(new Array(iteration), () => 0)
stop = Date.now();
console.log("Array.from - ES6 from @Zohaib ijaz executed in "+(stop-start)+"ms");

// Array.from - Non-ES6 from @Zohaib ijaz
start = Date.now();
array = Array.from(new Array(iteration), function(){return 0;})
stop = Date.now();
console.log("Array.from - Non-ES6 from @Zohaib ijaz executed in "+(stop-start)+"ms");

// repeat-split-map by @nicael
start = Date.now();
array = '0'.repeat(iteration).split('').map(Number);
stop = Date.now();
console.log("repeat-split-map by @nicael executed in "+(stop-start)+"ms");

// Fill
start = Date.now();
array = (new Array(iteration)).fill(0);
stop = Date.now();
console.log("Fill executed in "+(stop-start)+"ms");