函数
2025-08-06 16:38 更新
无论是函数声明还是函数表达式,(
前不要空格,但{
前一定要有空格;
函数调用括号前不需要空格;
立即执行函数外必须包一层括号;
不要给 inline function 命名;
参数之间用,
分隔,注意逗号后有一个空格。
示例代码:
// no space before '(', but one space before'{'
var doSomething = function(item) {
// do something
};
function doSomething(item) {
// do something
}
// not good
doSomething (item);
// good
doSomething(item);
// requires parentheses around immediately invoked function expressions
(function() {
return 1;
})();
// not good
[1, 2].forEach(function x() {
...
});
// good
[1, 2].forEach(function() {
...
});
// not good
var a = [1, 2, function a() {
...
}];
// good
var a = [1, 2, function() {
...
}];
// use ', ' between function parameters
var doSomething = function(a, b, c) {
// do something
};
以上内容是否对您有帮助:
← 变量声明
更多建议: