如果我想 "use" 提升,使用函数表达式而不是常规函数声明有缺点吗?
If I want to "use" hoisting, is there a downside to using function expressions instead of regular function declarations?
我正在学习 JavaScript,我觉得我对提升已经足够了解了,所以我不会问它是什么或如何做之类的。
吊装好不好?如果可以的话,我应该使用
声明我的变量吗?
var foo = function() {};
或者我应该改用这个吗?
function foo() {}
什么时候应该提升,什么时候不应该提升?或者它根本不重要?
提升不是你能做或不能做的事情,它只是发生在JavaScript。
提升是指所有变量声明 'moved' 到包含范围的顶部。
您所说的是使用函数表达式与函数声明。两种样式都很好,请记住它们在悬挂的内容上有细微差别:
var foo = function() {} // foo gets hoisted, but not the function itself
function foo(){} // the variable and the function get hoisted
有关更多信息,您可以查看我在 JavaScript 中写的一篇关于提升的文章:http://www.kenneth-truyers.net/2013/04/20/javascript-hoisting-explained/
在函数顶部声明所有变量是一种很好的做法,因为提升....(不是在 ES6 中)。
var foo = function() {};
和 function foo() {}
是两个不同的东西。第一个创建一个变量并传递一个匿名函数,第二个是 'just' 一个函数。
你不能说,什么绝对是更好的。这取决于上下文。
此问题没有 objective 答案。这归结为个人喜好。
在我自己的代码中,我更喜欢var foo = function() {};
。恕我直言,避免函数提升使代码更容易理解,因为源代码顺序很重要。
更新: ES6/ES2015 规范 has the same recommendation 针对特定情况:
Prior to ECMAScript 2015, the ECMAScript specification did not define the occurrence of a FunctionDeclaration as an element of a Block statement’s StatementList. However, support for that form of FunctionDeclaration was an allowable extension and most browser-hosted ECMAScript implementations permitted them. Unfortunately, the semantics of such declarations differ among those implementations. Because of these semantic differences, existing web ECMAScript code that uses Block level function declarations is only portable among browser implementation if the usage only depends upon the semantic intersection of all of the browser implementations for such declarations.
例如,以下代码的行为在 ES5 中未定义,并且在 browsers/engines/implementations:
之间有所不同
if (true) {
function foo() { return 1; }
} else {
function foo() { return 2; }
}
console.log(foo()); // `1` or `2`?
但是,以下代码具有完美定义和可互操作的行为:
var foo;
if (true) {
foo = function() { return 1; }
} else {
foo = function() { return 2; }
}
console.log(foo()); // `1`
TL;DR 尽可能避免函数提升。
我正在学习 JavaScript,我觉得我对提升已经足够了解了,所以我不会问它是什么或如何做之类的。
吊装好不好?如果可以的话,我应该使用
声明我的变量吗?var foo = function() {};
或者我应该改用这个吗?
function foo() {}
什么时候应该提升,什么时候不应该提升?或者它根本不重要?
提升不是你能做或不能做的事情,它只是发生在JavaScript。
提升是指所有变量声明 'moved' 到包含范围的顶部。
您所说的是使用函数表达式与函数声明。两种样式都很好,请记住它们在悬挂的内容上有细微差别:
var foo = function() {} // foo gets hoisted, but not the function itself
function foo(){} // the variable and the function get hoisted
有关更多信息,您可以查看我在 JavaScript 中写的一篇关于提升的文章:http://www.kenneth-truyers.net/2013/04/20/javascript-hoisting-explained/
在函数顶部声明所有变量是一种很好的做法,因为提升....(不是在 ES6 中)。
var foo = function() {};
和 function foo() {}
是两个不同的东西。第一个创建一个变量并传递一个匿名函数,第二个是 'just' 一个函数。
你不能说,什么绝对是更好的。这取决于上下文。
此问题没有 objective 答案。这归结为个人喜好。
在我自己的代码中,我更喜欢var foo = function() {};
。恕我直言,避免函数提升使代码更容易理解,因为源代码顺序很重要。
更新: ES6/ES2015 规范 has the same recommendation 针对特定情况:
Prior to ECMAScript 2015, the ECMAScript specification did not define the occurrence of a FunctionDeclaration as an element of a Block statement’s StatementList. However, support for that form of FunctionDeclaration was an allowable extension and most browser-hosted ECMAScript implementations permitted them. Unfortunately, the semantics of such declarations differ among those implementations. Because of these semantic differences, existing web ECMAScript code that uses Block level function declarations is only portable among browser implementation if the usage only depends upon the semantic intersection of all of the browser implementations for such declarations.
例如,以下代码的行为在 ES5 中未定义,并且在 browsers/engines/implementations:
之间有所不同if (true) {
function foo() { return 1; }
} else {
function foo() { return 2; }
}
console.log(foo()); // `1` or `2`?
但是,以下代码具有完美定义和可互操作的行为:
var foo;
if (true) {
foo = function() { return 1; }
} else {
foo = function() { return 2; }
}
console.log(foo()); // `1`
TL;DR 尽可能避免函数提升。