のための式:!!私はJavaScriptの忍者</strong>のジョンResig氏の<strong>秘密を読み、このコードを見たブール
function Ninja(){
this.swung = false;
// Should return true
this.swingSword = function(){
return !!this.swung;
};
}
私は!!
がブールへの発現を変換するために使用されて知っています。しかし、私の質問は、なぜ彼が使用していている:swung
はすでにブール変数であるか、私は何かが欠けていますので、
return !!this.swung;
はその冗長ではないですか?
function Ninja(){
this.swung = false;
// Should return true
this.swingSword = function(){
return !!this.swung;
};
}
// Should return false, but will be overridden
Ninja.prototype.swingSword = function(){
return this.swung;
};
var ninja = new Ninja();
assert(ninja.swingSword(), "Calling the instance method, not the prototype method."
)
ただ、推測は - 私はそれはあなたが 'swingSword'関数はブール値を返します知っているように、読みやすくするためだと思います。 –
"trueを返すべきです"というのは、奇妙なコメントです...本当に返さなければならない場合は、単にtrueを返すことができます。右? – Rudie