提问人:Ajedama 提问时间:11/11/2023 更新时间:11/12/2023 访问量:70
箭头函数作为 JavaScript 中函数的参数
Arrow function as a parameter to a function in JavaScript
问:
我是一名初级/中级 JavaScript 程序员,最近在求职面试中分发了一些练习。
我必须用 JavaScript 编写一个名为 find 的函数,它接受两个参数,其中一个是箭头函数,包含一个参数。该函数应在数组中找到适当的对象并返回它。我很难理解箭头函数((toy) => (toy.type === 'car')) 的作用,它如何计算,我如何使用它。玩具没有定义,从示例中可以看出,它只是用作参数,我觉得非常令人困惑和费解。
给出的练习:
const toys = [{type: "car", color: "red"}, {type: "ball", color: "blue"}, {type: "teddy-bear", color: "brown"}]
const carToy = find(toys, (toy) => (toy.type === 'car'));
console.log(carToy); // expected output: {type: "car", color: "red"}
我写了什么:
function find(array, toy) {
console.log(toy); // [Function (anonymous)]
toy(); // TypeError: Cannot read properties of undefined (reading 'type')
console.log(toy()); // TypeError: Cannot read properties of undefined (reading 'type')
}
起初我以为玩具会评估到字符串“汽车”,但事实并非如此。查找函数中的 console.log() 返回 undefined 或 [函数(匿名)]。
如果我得到一个字符串,我可以轻松解决问题,但箭头函数让我卡住了一个多小时。我试图在网上找到解决方案,但没有什么能帮助我理解这一点。
很想得到一些关于如何解决这个问题的指示,以及任何可以教我更多关于箭头函数的深入资源。
答:
看起来您需要实现 find 方法。
发生的情况是,该函数是作为参数传递的,因此为了能够调用该函数,您需要向它传递一个 toy 类型的对象。
像这样的东西,没有测试它,所以可能需要一两个调整
function find(array, toy) {
for(var i in array) {
if(toy(array[i])
return array[i];
}
return null;
}
您可以阅读本文以获取有关此主题的教程
评论
“箭头”函数的名称实际上是创建匿名函数的一种形式,所以这就是返回它的原因。
匿名函数是没有名称的函数。您可以通过其变量调用它,而不是通过其函数名称(后跟 2 个括号的名称:“()”)
JavaScript 有很多方法可以做同样的事情。作为初学者,很容易感到困惑并理解其中的一些差异。
我必须在 JavaScript 中编写一个名为 find 的函数,它接受两个参数,其中一个是箭头函数,包含一个参数
首先要注意的是,箭头函数与普通函数的区别仅在于语法和上下文。每个箭头函数都可以表示为非箭头函数。有关细微差异的一些说明,您可以阅读箭头函数文档 -
- 从父作用域继承
this
- 没有或
arguments
super
- 不能用作构造函数
- 不能用作生成器功能
我很难理解箭头函数的作用
((toy) => (toy.type === 'car'))
直观地说,这是一个功能,说,给我一个玩具,我会告诉你它是否是一辆汽车。
这里它作为一个函数表达式 -
(function(toy) {
return toy.type === "car"
})
也许将其视为命名函数会有所帮助 -
function isCar(toy) {
return toy.type === "car"
}
起初我以为玩具会评估到字符串“汽车”,但事实并非如此
运算符返回 ,而不是字符串。仅当等于字符串文字时,该函数才会返回。===
boolean
true
toy.type
"car"
在您的程序中 -
function find(array, toy) {
console.log(toy); // [Function (anonymous)]
toy(); // TypeError: Cannot read properties of undefined (reading 'type')
console.log(toy()); // TypeError: Cannot read properties of undefined (reading 'type')
}
谈论起来有点令人困惑,因为——
- 在本练习中,箭头函数有一个参数,名为
toy
- 在您的尝试中,箭头函数的名称也是
toy
让我们写下我们的观点,并就我们现在的理解发表一些评论——fn
function find(array, fn) {
// given fn is a function that takes a item and returns a boolean
// given array is an array of items
// find the item where fn(item) is true
// look in the items
for (const item of array) {
// complete the program ...
}
}
值得一提的是,你应该努力用 -
- 定义明确的输入
- 定义明确的输出
- 无副作用(如、等)
console.log
throw
为该函数编写一些基本文档是有帮助的。常见的格式是 jsDoc -
/**
* Searches for the first element in an array that satisfies the provided testing function.
* @param {Array} items - The array to search through.
* @param {function} fn - The testing function, which should return `true` for the desired element.
* @returns {any|null} Returns the first element that satisfies the provided testing function, or `null` if no such element is found.
*/
function find(items, fn) {
// ...
}
我发现当所有这些项目都清晰地摆放时,思考起来要容易得多。其他工具更进一步,实现完整的类型系统,以确保实现的正确性。TypeScript 中的此类示例如下所示
function find<T>(items: Array<T>, fn: (item: T) => boolean): null | T {
for (const item of items) {
// ...
}
return null
}
即使你为代码选择这两种方法,你仍然应该在实践中考虑这些原则。函数对于构建良好的抽象至关重要,因此我们学习好的模式以及如何识别坏的模式至关重要。
评论
find(array, toy)
- 第二个参数不是“玩具”,而是一个函数。尝试在代码中使用更有意义的名称。