我最近开始保持别人的JavaScript代码,我正在修复错误,添加功能,也试图更新代码并使其更加一致。

以前的开发人员使用了两种方式来宣布功能,我无法解决是否有原因。

兩種方式是:

var functionOne = function() {
    // Some code
};
function functionTwo() {
    // Some code
}

使用这两种不同的方法的原因是什么?每个方法的优点和缺点是什么?可以用一种方法做些什么,不能用另一种方法做些什么?


当前回答

这就是所谓的函数表达:

var getRectArea = function(width, height) {
    return width * height;
};

console.log("Area of Rectangle: " + getRectArea(3,4));
// This should return the following result in the console: 
// Area of Rectangle: 12

这就是所谓的功能声明:

var w = 5;
var h = 6;

function RectArea(width, height) {  //declaring the function
  return area = width * height;
}                                   //note you do not need ; after }

RectArea(w,h);                      //calling or executing the function
console.log("Area of Rectangle: " + area);
// This should return the following result in the console: 
// Area of Rectangle: 30

希望这有助于解释函数表达和函数声明之间的区别,以及如何使用它们。

其他回答

关于表演:

新版本的V8引入了几个未来的优化,所以SpiderMonkey。

现在表达与声明之间几乎没有区别,功能表达似乎现在更快。

浏览器 62.0.3202

福克斯55

Chrome 加拿大 63.0.3225

此分類上一篇

匿名函数表达式似乎比名函数表达式表现更好。

此分類上一篇

此分類上一篇: Firefox Chrome Canary Chrome

在 Debugger/DevTools 中,如果您在控制台中使用格式函数Name() {} 函数时,您不能随后在控制台中使用函数Name() (即“未定义”),而在 var 函数Name = 函数() {} 后,您可以使用函数。

看这个问题。

在代码维护成本方面,所谓的功能更受欢迎:

无论它们在哪里被宣布(但仍然受到范围限制)。 更抵抗错误,如条件启动(你仍然可以过度排序,如果你愿意)。 代码变得更可读,通过分配地方功能分开的范围功能。 通常在范围内,功能先行,其次是局部功能的声明。

我怀疑更多关于名称功能的PROS正在追踪,而被列为名称功能的优势是匿名功能的缺点。

历史上,匿名函数出现,因为JavaScript作为语言无法列出名为函数的会员:

{
    member:function() { /* How do I make "this.member" a named function? */
    }
}

区别在于,函数One 是一种函数表达式,因此只有在到达该行时才定义,而函数Two 是一种函数声明,并在其周围函数或脚本执行后才定义。

例如,函数表达式:

// TypeError: functionOne 不是函数函数One(); var 函数One = 函数() { console.log(“Hello!”); };

还有一个功能声明:

// 输出: “Hello!”函数Two();函数函数Two() { console.log(“Hello!”); }

“使用严格”; { // 注意此区块! 函数三() { console.log(“Hello!”); } } 函数三(); // 参考错误

a = 10
output : 10

(1 + 3)
output = 4

声明/声明:不返回值的东西 示例:

if (1 > 2) {
 // do something. 
}

此分類上一篇

同样,我们有函数声明/声明 vs 函数表达 请举一个例子:

// test.js

var a = 10;

// function expression
var fun_expression = function() {
   console.log("Running function Expression");
}

// funciton expression

function fun_declaration() {
   console.log("Running function Statement");
}

重要:在JavaScript引擎运行上面的JS文件时会发生什么。

现在,假设我们更新JS到。

// test.js

console.log(a)  //output: udefined (No error)
console.log(fun_expression)  // output: undefined (No error)
console.log(fun_expression()) // output: Error. As we trying to invoke undefined. 
console.log(fun_declaration()) // output: running function statement  (As fun_declaration is already hoisted in the memory). 

var a = 10;

// function expression
var fun_expression = function() {
   console.log('Running function expression')
}

// function declaration

function fun_declaration() {
   console.log('running function declaration')
}

console.log(a)   // output: 10
console.log(fun_expression()) //output: Running function expression
console.log(fun_declaration()) //output: running function declaration

在评论中提到的结果,应该有助于理解函数表达与函数声明/声明之间的区别。