函数表达式
//匿名函数
var anonymous = function(){
return true;
};
//命名函数
var named = function named(){
return true;
}
//IIFE 函数 Immediately-Invoked Function Expression
(function(){
console.log('Hello!');
})();
不要再非函数块中(if while etc)声明函数,尽管浏览器允许你分配给函数一个变量,但是不同的浏览器解析方式可能不同。
ECMA-262把块定义为一组语句,但函数不是一个语句。
//good
var test ;
if(aaa){
test = function test(){
console.log('Hi~~');
};
}
//bad
if(aaa){
function test(){
console.log('???');
}
}
不要命名一个参数为arguments,否则他将优先于传递给每个函数作用域中的arguments对象。
//good
function aaa(name,sexual,age){
}
//bad
function bbb(name,sexual,arguments){
}
1:5 And God called the light day,and the darkness he called Night.And the evening and the morning were the first day.