2010-07-13 18 views

回答

6

聲明一個功能是指當腳本塊進行解析,它的定義,而其分配給一個變量在運行時完成:

x(); // this works as the function is defined before the script block is executed 

function x() {} 

但:

x(); // doesn't work as x is not assigned yet 

var x = function() {} 

將函數賦值給變量可以有條件地完成。例如:

var getColor; 
if (color == 'red') { 
    getColor = function() { return "Red"; } 
} else { 
    getColor = function() { return "Blue"; } 
} 
相關問題