JS & function declaration function expression
There are two ways-defined function: function declarations and function expressions
Function declarations
There is an important characteristic: the function declaration upgrade (click to see) , which means before the execution context function reads the statement, the function can be called before the function declaration
functionName(); //可以在函数声明前就调用
function functionName(){
//your code
}
Function expression
Examples
myFun(); //报错,必须在表达式后面
var myFun=function(){
return "aa";
}
Usage Example
var encoding=1;
var myFun;
if(encoding==1){
myFun=function(){
return "你好";
}
}else{
myFun=function(){
return "Hello";
}
}