I read John Resig Ninja JavaScript Secrets and saw this code:
function Ninja(){ this.swung = false; // Should return true this.swingSword = function(){ return !!this.swung; }; }
I know that !! used to convert an expression to boolean. But my question is why it uses:
return !!this.swung;
Isn't that redundant because swung already a boolean variable or am I missing something?
By the way, here is the complete code just in case:
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." )
source share