Javascript array.prototype.map when/why 使用第三个参数
Javascript array.prototype.map when/why use third argument
我最近注意到,在使用 array.prototype.map()
时,我的回调函数可能会接受第三个参数,即我正在映射的数组。我想知道这个参数是否有任何用例,因为在回调函数中,无论如何都可以访问数组。
如果我访问我在回调函数中映射的数组,why/when我应该使用第三个参数而不是仅仅访问数组吗?
尽管这两种方法在我能想象到的每个用例中都可以正常工作,但我想知道哪种是推荐的方法以及原因。
编辑: 这个答案已经(巧合地)在标记的欺骗中给出了。检查一下 。
您可能希望以通用方式引用原始数组。例如,回调可能是一个单独的函数,而不是通常看到的匿名函数:
function arrayMapper(val, idx, arr) {
// Some operations which use arr
}
myArray.map(arrayMapper);
myOtherArray.map(arrayMapper);
注意每个在执行 arrayMapper
时,arr
参数如何引用外部作用域中的不同数组。
当您链接多个数组方法并需要访问数组的中间状态(前一个操作的结果)时,第三个参数很有用:
const source = [-3,-2,-1,0,1,2,3,4,5];
source
.filter(n => n >= 0)
.map((n, index, arr) => {
// arr contains only non-negative numbers
// here you may have some logic that rely on it
return n;
})
您正在使用正常的全局上下文,但不需要 arise.See 下面的示例中您 不是 使用 arrow 函数(保留当前上下文)与面向对象的函数编程。当时它帮助第三属性。在第一个示例中,我无法在匿名函数中访问 this.numbers,因为匿名函数有自己的上下文。当您想访问原始数组以进行操作时,它在这里很有用
工作 示例:
function Demo (numbers){
this.numbers = numbers;
}
Demo.prototype.doMap = function () {
return this.numbers.map(function(num, index, array) {
return array[num]
});
}
var obj = new Demo([1, 2, 4, 2, 3]);
console.log(obj.doMap()); // will work
非工作示例 如果我不传递数组,它会抛出错误。
function Demo (numbers){
this.numbers = numbers;
}
Demo.prototype.doMap = function () {
return this.numbers.map(function(num, index, array) {
return this.numbers[num] // cant access original number array using this (this refers to context of anonymous function)
});
}
var obj = new Demo([1, 2, 4, 2, 3]);
console.log(obj.doMap()); // throw error
我最近注意到,在使用 array.prototype.map()
时,我的回调函数可能会接受第三个参数,即我正在映射的数组。我想知道这个参数是否有任何用例,因为在回调函数中,无论如何都可以访问数组。
如果我访问我在回调函数中映射的数组,why/when我应该使用第三个参数而不是仅仅访问数组吗?
尽管这两种方法在我能想象到的每个用例中都可以正常工作,但我想知道哪种是推荐的方法以及原因。
编辑: 这个答案已经(巧合地)在标记的欺骗中给出了。检查一下
您可能希望以通用方式引用原始数组。例如,回调可能是一个单独的函数,而不是通常看到的匿名函数:
function arrayMapper(val, idx, arr) {
// Some operations which use arr
}
myArray.map(arrayMapper);
myOtherArray.map(arrayMapper);
注意每个在执行 arrayMapper
时,arr
参数如何引用外部作用域中的不同数组。
当您链接多个数组方法并需要访问数组的中间状态(前一个操作的结果)时,第三个参数很有用:
const source = [-3,-2,-1,0,1,2,3,4,5];
source
.filter(n => n >= 0)
.map((n, index, arr) => {
// arr contains only non-negative numbers
// here you may have some logic that rely on it
return n;
})
您正在使用正常的全局上下文,但不需要 arise.See 下面的示例中您 不是 使用 arrow 函数(保留当前上下文)与面向对象的函数编程。当时它帮助第三属性。在第一个示例中,我无法在匿名函数中访问 this.numbers,因为匿名函数有自己的上下文。当您想访问原始数组以进行操作时,它在这里很有用
工作 示例:
function Demo (numbers){
this.numbers = numbers;
}
Demo.prototype.doMap = function () {
return this.numbers.map(function(num, index, array) {
return array[num]
});
}
var obj = new Demo([1, 2, 4, 2, 3]);
console.log(obj.doMap()); // will work
非工作示例 如果我不传递数组,它会抛出错误。
function Demo (numbers){
this.numbers = numbers;
}
Demo.prototype.doMap = function () {
return this.numbers.map(function(num, index, array) {
return this.numbers[num] // cant access original number array using this (this refers to context of anonymous function)
});
}
var obj = new Demo([1, 2, 4, 2, 3]);
console.log(obj.doMap()); // throw error