我有一个函数read()
需要作为布尔参数。如果false
传入 - read(false)
- 它不应该运行一段代码。它适用于以下三个变体,但我不确定它们之间的差异或者它是否重要?将函数的默认参数设置为布尔值的正确方法?
但我不明白这些变化之间的差异。
所有这三种变化的工作。
this.first_time_here = first_time_here !== false;
var first_time_here = first_time_here !== false;
var first_time_here = first_time_here || false;
读功能
function read (first_time_here) {
var first_time_here = first_time_here !== false;
// check to see what the function thinks first_time_here is
console.log("first time here is: " + first_time_here);
if (typeof first_time_here === 'boolean') {
console.log('Yes, I am a boolean');
}
if (first_time_here) {
// do something if true
};
};
谢谢
甜和明确的。感谢您的增加版本。我会用它来创建更强大的API。 – 2013-05-05 03:21:10