Javascript功能如“var foo = function bar()...”? [英] Javascript functions like "var foo = function bar() ..."?

查看:90
本文介绍了Javascript功能如“var foo = function bar()...”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是这样的(语法看起来可能很奇怪,但据我所知,它没有任何问题,或者是有?)

  var add = function addNums(a,b){
return a + b;
}
alert(add:+ add(2,3)); //产生5
alert(addNums:+ addNums(2,3)); //也应该产生5

addNums()被声明为一个函数。所以,当我传递参数给它时,它也应该返回结果。



然后,为什么我没有得到第二个警告框?

解决方案

您看到命名函数表达式(NFE)

匿名函数表达式是指派一个没有名字的函数的地方变量 1

  var add = function(){
console.log 我没有自己的名字。);



$ b $ p
$ b

命名函数表达式是将一个命名函数赋值给一个变量!):

  var add = function addNums(){
console.log(我的名字是addNums,但是只有我会知道。);
}

该函数的名称仅在函数本身中可用。这使得您可以在不必知道函数的外部名称的情况下使用递归 - 即使不需要首先设置一个(认为回调函数)。



您选择的名称​​阴影现有名称,因此如果其他地方定义了另一个 addNums ,它将不会被覆盖。这意味着你可以毫不畏惧地使用任何你喜欢的名字来确定问题范围或破坏任何东西。



在过去,你会使用 arguments.callee 来引用一个函数内部不知道它的名字。但是对JavaScript的支持正在从JavaScript 2 中去除,所以NFE是现在这样做的正确方法。



这里有很多东西阅读以下主题: http://kangax.github.io/nfe/






1 将它赋值给一个变量不是必须的,它只是作为一个例子来区分它和普通函数声明。它可以是JS预期表达式(例如函数参数)的任何其他上下文。



2 如果您遇到错误严格模式有效并尝试使用 arguments.callee


The code goes like this (The syntax may seem odd but as far as I know, there is nothing wrong with it. Or is there?)

var add=function addNums(a, b) {                     
   return a+b;
 }                     
 alert("add: "+ add(2,3));           // produces 5
 alert("addNums: "+addNums(2,3));        // should also produce 5

addNums() is declared as a function. So, when I pass the parameters to it, it should also return the result.

Then, why am I not getting the second alert box?

解决方案

You are seeing a named function expression (NFE).

An anonymous function expression is where you assign a function without a name to a variable1:

var add = function () {
  console.log("I have no own name.");
}

A named function expression is where you assign a named function to a variable (surprise!):

var add = function addNums() {
  console.log("My name is addNums, but only I will know.");
}

The function's name is only available within the function itself. This enables you to use recursion without necessarily knowing the "outside name" of the function - even without having to set one in the first place (think callback functions).

The name you choose shadows an existing name, so if another addNums is defined elsewhere it will not be overridden. This means you can use any name you like without fear for scoping problems or breaking anything.

In the past you would have used arguments.callee to refer to a function inside itself without knowing its name. But support for that is being removed from JavaScript2, so NFEs are the correct way to do this nowadays.

Here is a lot of stuff to read on the topic: http://kangax.github.io/nfe/


1 Assigning it to a variable is not necessary, it just serves as an example to distinguish it from a plain function declaration. It could be any other context where JS expects an expression (a function argument, for example).

2 You will receive an error if you have strict mode in effect and try to use arguments.callee.

这篇关于Javascript功能如“var foo = function bar()...”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆