箭头函数可以作为 repeat() 的参数传递吗?
Can arrow functions be passed as parameters of repeat()?
在搜索箭头函数时,我遇到了这个 example
let labels = [];
repeat(5, i => {
labels.push(`Unit ${i + 1}`);
});
console.log(labels);
// → ["Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5"]
1st,MDN 的 repeat 方法似乎只接受一个参数 (count)
。
2、箭头函数应该这样写:i = () => {}
,而不是i => {}
这是一个过时的片段,无论如何都不起作用,还是我应该以另一种方式解释它?
它看起来像 repeat
的自有函数,而不是 String
的函数。
在这种情况下,您可以将带有所需参数签名的函数传递给函数。
const
repeat = (l, fn) => {
var i;
for (i = 0; i < l; i++) fn(i);
};
let labels = [];
repeat(5, i => labels.push(`Unit ${i + 1}`));
console.log(labels); // → ["Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5"]
文中提到的repeat
方法不是String.prototype.repeat
,而是文中定义的独立函数——不是内置函数:
But what if we want to do something other than logging the numbers? Since “doing something” can be represented as a function and functions are just values, we can pass our action as a function value.
function repeat(n, action) {
for (let i = 0; i < n; i++) {
action(i);
}
}
repeat(3, console.log);
你提到的语法
arrow functions should be written like this: i = () => {}, and not i => {}
仅当所讨论的箭头函数旨在接受正好为零的参数时才为真。带有一个参数 的箭头函数可以 从参数列表中省略括号。 (两个或多个参数再次需要括号)
您提到的片段确实可以正常工作:
function repeat(n, action) {
for (let i = 0; i < n; i++) {
action(i);
}
}
let labels = [];
repeat(5, i => {
labels.push(`Unit ${i + 1}`);
});
console.log(labels);
首先你说的repeat built-in函数只接受一个参数,所以这里使用的repeat函数不是built-in而是user-defined函数
箭头函数可以写成i = () => {}
但是这里是匿名箭头函数,不需要名字,我们也可以这样写repeat函数:
repeat(5, function(i) { labels.push(i) })
箭头函数看起来可读性强,所以才这么写。
如果您对 for-loop 中的语句感到困惑,那么在 for-loop
中调用的是 repeat 函数
也许会有帮助。
在搜索箭头函数时,我遇到了这个 example
let labels = [];
repeat(5, i => {
labels.push(`Unit ${i + 1}`);
});
console.log(labels);
// → ["Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5"]
1st,MDN 的 repeat 方法似乎只接受一个参数 (count)
。
2、箭头函数应该这样写:i = () => {}
,而不是i => {}
这是一个过时的片段,无论如何都不起作用,还是我应该以另一种方式解释它?
它看起来像 repeat
的自有函数,而不是 String
的函数。
在这种情况下,您可以将带有所需参数签名的函数传递给函数。
const
repeat = (l, fn) => {
var i;
for (i = 0; i < l; i++) fn(i);
};
let labels = [];
repeat(5, i => labels.push(`Unit ${i + 1}`));
console.log(labels); // → ["Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5"]
文中提到的repeat
方法不是String.prototype.repeat
,而是文中定义的独立函数——不是内置函数:
But what if we want to do something other than logging the numbers? Since “doing something” can be represented as a function and functions are just values, we can pass our action as a function value.
function repeat(n, action) {
for (let i = 0; i < n; i++) {
action(i);
}
}
repeat(3, console.log);
你提到的语法
arrow functions should be written like this: i = () => {}, and not i => {}
仅当所讨论的箭头函数旨在接受正好为零的参数时才为真。带有一个参数 的箭头函数可以 从参数列表中省略括号。 (两个或多个参数再次需要括号)
您提到的片段确实可以正常工作:
function repeat(n, action) {
for (let i = 0; i < n; i++) {
action(i);
}
}
let labels = [];
repeat(5, i => {
labels.push(`Unit ${i + 1}`);
});
console.log(labels);
首先你说的repeat built-in函数只接受一个参数,所以这里使用的repeat函数不是built-in而是user-defined函数
箭头函数可以写成
i = () => {}
但是这里是匿名箭头函数,不需要名字,我们也可以这样写repeat函数:repeat(5, function(i) { labels.push(i) })
箭头函数看起来可读性强,所以才这么写。
如果您对 for-loop 中的语句感到困惑,那么在 for-loop
中调用的是 repeat 函数也许会有帮助。