将匿名函数作为参数传递,但它将其视为字符串。

huangapple go评论98阅读模式
英文:

Passing an anonymous function as an argument but it treats it as a string

问题

  1. 我对JavaScript/编程是新手,只是为了熟悉概念而尝试将函数作为参数传递。我在网上看到了下面的例子,然后尝试创建了一个更简单的例子:
  2. ```js
  3. var materialsLength1 = materials.map(function(material) {
  4. return material.length;
  5. });

这是我尝试的部分:

  1. function appendLetterToString(letter, str) {
  2. return str += letter
  3. }
  4. let pizza = appendLetterToString("a", function(){
  5. return "pizz";
  6. })
  7. console.log(pizza);

我期望在控制台中得到 'pizza',但我得到的结果是这个字符串:

  1. function(){
  2. return "pizz";
  3. }a

为什么我的 'function' 被评估为一个字符串?希望这不是一个愚蠢的问题。感谢任何答案!

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;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;
});

  1. Here&#39;s what I tried:
  2. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  3. &lt;!-- language: lang-js --&gt;
  4. function appendLetterToString(letter, str) {
  5. return str += letter
  6. }
  7. let pizza = appendLetterToString(&quot;a&quot;, function(){
  8. return &quot;pizz&quot;;
  9. })
  10. console.log(pizza);
  11. &lt;!-- end snippet --&gt;
  12. I was expecting to get &#39;pizza&#39; in the console but the result I got is this string:

function(){
return "pizz";
}a

  1. Why does my &#39;function&#39; get evaluated as a string?
  2. Hope this isn&#39;t a dumb question. Appreciate any answers!
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. &gt; 为什么我的 'function' 会被解释为一个字符串?
  7. 因为你没有运行这个函数,所以值不是 `pizz`,而是函数本身,作为一个字符串。
  8. ----------
  9. 在你的例子中,没有必要将第二个参数传递为一个函数,但如果你想要这样做,你应该调用它以获取它的返回值:
  10. ```js
  11. function appendLetterToString(letter, str) {
  12. return str() + letter
  13. }
  14. let pizza = appendLetterToString("a", function(){
  15. return "pizz";
  16. })
  17. console.log(pizza);

但你可能更好地传递一个字符串:

  1. function appendLetterToString(letter, str) {
  2. return str += letter
  3. }
  4. let pizza = appendLetterToString("a", "pizz");
  5. 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 -->

  1. function appendLetterToString(letter, str) {
  2. return str() + letter
  3. }
  4. let pizza = appendLetterToString(&quot;a&quot;, function(){
  5. return &quot;pizz&quot;;
  6. })
  7. 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 -->

  1. function appendLetterToString(letter, str) {
  2. return str += letter
  3. }
  4. let pizza = appendLetterToString(&quot;a&quot;, &quot;pizz&quot;);
  5. console.log(pizza);

<!-- end snippet -->

答案2

得分: 0

匿名函数的工作方式与具名函数相同:必须显式调用它们;在表达式中使用时不会自动调用它们。

所以,如果你想让一个方法接受匿名函数作为参数,你必须在方法内部编写代码来调用该函数。

例如,如果你希望str成为一个函数,你可以像这样编写append函数:

  1. function appendLetterToString(letter, str) {
  2. return str() += letter
  3. }

现在它将仅在你传递一个函数时工作,如果你传递一个字符串将不起作用。如果你希望它两种方式都能工作,你必须检查体内值的类型:

  1. function appendLetterToString(letter, strOrFun) {
  2. let str = strOrFun
  3. if (typeof strOrFun === 'function')
  4. str = strOrFun()
  5. return str += letter
  6. }
英文:

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:

  1. function appendLetterToString(letter, str) {
  2. return str() += letter
  3. }

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:

  1. function appendLetterToString(letter, strOrFun) {
  2. let str = strOrFun
  3. if (typeof strOrFun === &#39;function&#39;)
  4. str = strOrFun()
  5. return str += letter
  6. }

huangapple
  • 本文由 发表于 2023年4月17日 21:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定