codecamp

函数

无论是函数声明还是函数表达式,(前不要空格,但{前一定要有空格;
函数调用括号前不需要空格;
立即执行函数外必须包一层括号;
不要给 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
};
变量声明
数组、对象
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

编辑器配置和构建检查

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }