2015-01-20 189 views

回答

4

試試這個

var myFunction = function(variable) { 
    variable = variable || 0; 
    // this variant works for all falsy values "", false, null, undefined... 
    // if you need check only undefiend, use this variant 
    // variable = typeof(variable) == 'undefined' ? 0 : variable 
}; 

Default function parameters將在ES6; ES5不允許它。

+3

這不正是* *一樣,雖然PHP代碼。你想使用'variable = typeof(variable)==='undefined'? 0:變量'。 – h2ooooooo 2015-01-20 19:32:26

+0

@ h2ooooooo感謝您的評論,我也添加了你的變體。 – 2015-01-20 19:40:16