更新时间:2022-12-27 12:11:20
注意 ve发布为社区wiki,我们都可以添加到列表中,澄清等。请不要意见。保持客观。
或者这只是一个味道/风格指南等的问题?
风格有很强的影响,是的,但是有一些客观的观察,我们可以在功能和运行时特性方面进行选择,这些选项可以用来决定哪个是适用于给定的用例。
选项1:
function square(n){
return n * n;
}
square = ...
(或稍后的函数声明)。 square.prototype
,即使我们不打算作为一个构造函数。在函数中使用了这个
,那么它将由函数调用决定,因为它是一个正常函数。选项2:
var square = function(n){
return n * n;
};
square = ...
square.prototype
,即使我们不打算将其作为构造函数。 new square )将工作,但可能不会执行编码器的预期。
var
变量。这个
功能,它将由函数调用决定,因为它是一个正常函数。选项2.5:(我添加了这个)
var square = function square(n ){
return n * n;
};
完全类似于选项2,除了在ES5和更早版本,函数有一个真实的名字( square
)。 (请注意,该名称不一定与变量的名称相同,尽管在此示例中。)
选项3:
var square =(n)=> {
return n * n;
};
还可以写:
var square = n => n * n;
square = ...
square.prototype
。
new square
)将失败,并显示一条信息错误( TypeError:square不是构造函数
)。参数
(但是如果需要参数
功能,则可以使用rest参数。 li> 这个
有参数
。但现代JavaScript引擎已经优化了参数
的创建,如果您不使用它,并且不太可能设置这个
是一个显着的成本。 var
变量这个
在函数中使用,它将使用由于箭头函数关闭 这个
这个这个 >(而不是按照他们的名字设定)。选项4: p>
const square =(n)=> {
return n * n;
};
还可以写:
const square = n => n * n;
square = ...
square.prototype
。 new square
)将失败,并显示一条内容丰富的错误( TypeError:square不是构造函数
)。参数
(参见选项3中的注释)。 const
。这个
在函数中使用,它将使用相同的这个
作为函数定义的代码,因为箭头 这个
(而不是通过调用它们来设置)。选项5:(我添加了这个)
let square =(n)=> {
return n * n;
};
还可以写:
let square = n => n * n;
与选项4完全相同,只能稍后通过 square =覆盖。 ..
What are the advantages/disadvantages to create a top level function in ES6/ES2015 in those different ways? Or is this just a matter of taste/style guide etc?
Option 1:
function square(n) {
return n * n;
}
Option 2:
var square = function(n) {
return n * n;
};
Option 3:
var square = (n) => {
return n * n;
};
Option 4:
const square = (n) => {
return n * n;
};
Note: I've posted this as a community wiki, we can all add to the list, clarify, etc. Please no opinions. Keep it objective.
Or is this just a matter of taste/style guide etc?
There will be a strong influence of style, yes, but there are some objective observations we can make in terms of the functionality and runtime characteristics of the options that can be used to decide which is appropriate for a given use-case.
Option 1:
function square(n) { return n * n; }
square = ...
(or a later function declaration).square.prototype
, even though we don't intend it to be a constructor.this
were used within the function, it would be determined by how the function is called, as it's a "normal" function.Option 2:
var square = function(n) { return n * n; };
square = ...
square.prototype
, even though we don't intend it to be a constructor.new square
) will work, but probably not do what the coder expected.var
variable.this
were used within the function, it would be determined by how the function is called, as it's a "normal" function.Option 2.5: (I've added this one)
var square = function square(n) { return n * n; };
Exactly like Option 2, except that on ES5 and earlier, the function has a true name (square
). (Note that the name doesn't have to be the same as the name of the variable, although it is in this example.)
Option 3:
var square = (n) => { return n * n; };
Could also be written:
var square = n => n * n;
square = ...
square.prototype
.new square
) will fail with an informative error (TypeError: square is not a constructor
).arguments
(but you can use rest arguments instead if you need arguments
functionality).this
and doesn't have arguments
. But modern JavaScript engines already optimize-out the creation of arguments
if you don't use it, and it's unlikely setting up this
is a significant cost.var
variable.this
were used within the function, it would use the same this
as the code where the function is defined, since arrow functions close over this
(rather than having it set by how they're called).Option 4:
const square = (n) => { return n * n; };
Could also be written:
const square = n => n * n;
square = ...
square.prototype
.new square
) will fail with an informative error (TypeError: square is not a constructor
).arguments
(see notes on Option 3).const
.this
were used within the function, it would use the same this
as the code where the function is defined, since arrow functions close over this
(rather than having it set by how they're called).Option 5: (I've added this one)
let square = (n) => { return n * n; };
Could also be written:
let square = n => n * n;
Exactly like Option 4, except it can be overwritten later via square = ...