英文:
Passing an anonymous function as an argument but it treats it as a string
问题
我对JavaScript/编程是新手,只是为了熟悉概念而尝试将函数作为参数传递。我在网上看到了下面的例子,然后尝试创建了一个更简单的例子:
```js
var materialsLength1 = materials.map(function(material) {
return material.length;
});
这是我尝试的部分:
function appendLetterToString(letter, str) {
return str += letter
}
let pizza = appendLetterToString("a", function(){
return "pizz";
})
console.log(pizza);
我期望在控制台中得到 'pizza',但我得到的结果是这个字符串:
function(){
return "pizz";
}a
为什么我的 'function' 被评估为一个字符串?希望这不是一个愚蠢的问题。感谢任何答案!
<details>
<summary>英文:</summary>
I'm new to JavaScript/programming and I was playing around with passing functions as arguments just to get familiar with the concept. I saw the following example online and tried to create my own more simple example:
var materialsLength1 = materials.map(function(material) {
return material.length;
});
Here's what I tried:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function appendLetterToString(letter, str) {
return str += letter
}
let pizza = appendLetterToString("a", function(){
return "pizz";
})
console.log(pizza);
<!-- end snippet -->
I was expecting to get 'pizza' in the console but the result I got is this string:
function(){
return "pizz";
}a
Why does my 'function' get evaluated as a string?
Hope this isn't a dumb question. Appreciate any answers!
</details>
# 答案1
**得分**: 1
> 为什么我的 'function' 会被解释为一个字符串?
因为你没有运行这个函数,所以值不是 `pizz`,而是函数本身,作为一个字符串。
----------
在你的例子中,没有必要将第二个参数传递为一个函数,但如果你想要这样做,你应该调用它以获取它的返回值:
```js
function appendLetterToString(letter, str) {
return str() + letter
}
let pizza = appendLetterToString("a", function(){
return "pizz";
})
console.log(pizza);
但你可能更好地传递一个字符串:
function appendLetterToString(letter, str) {
return str += letter
}
let pizza = appendLetterToString("a", "pizz");
console.log(pizza);
英文:
> Why does my 'function' get evaluated as a string?
Because you don't run the function, so the value isn't pizz
, but the function itself, as a string.
In your example there is no need to pass the second arg as a function, but if you want that, you should call it to get it's return value:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function appendLetterToString(letter, str) {
return str() + letter
}
let pizza = appendLetterToString("a", function(){
return "pizz";
})
console.log(pizza);
<!-- end snippet -->
But you're probably better of passing a string:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function appendLetterToString(letter, str) {
return str += letter
}
let pizza = appendLetterToString("a", "pizz");
console.log(pizza);
<!-- end snippet -->
答案2
得分: 0
匿名函数的工作方式与具名函数相同:必须显式调用它们;在表达式中使用时不会自动调用它们。
所以,如果你想让一个方法接受匿名函数作为参数,你必须在方法内部编写代码来调用该函数。
例如,如果你希望str
成为一个函数,你可以像这样编写append函数:
function appendLetterToString(letter, str) {
return str() += letter
}
现在它将仅在你传递一个函数时工作,如果你传递一个字符串将不起作用。如果你希望它两种方式都能工作,你必须检查体内值的类型:
function appendLetterToString(letter, strOrFun) {
let str = strOrFun
if (typeof strOrFun === 'function')
str = strOrFun()
return str += letter
}
英文:
Anonymous functions work the same as named functions: you have to call them explicitly; they don't get called automatically when used in expressions.
So if you want a method to accept an anonymous function as an argument, you have to write the code inside the method to call the function.
For example, if you want str
to be a function, you could write the append function like this:
function appendLetterToString(letter, str) {
return str() += letter
}
Now it will work only if you pass a function, not if you pass a string. If you want it to work both ways, you have to examine the type of the value inside the body:
function appendLetterToString(letter, strOrFun) {
let str = strOrFun
if (typeof strOrFun === 'function')
str = strOrFun()
return str += letter
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论