Skip to content

Files

Latest commit

 

History

History

fooling_around_boolean

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

#1 Fooling around boolean

Look at the following "implementation" of a xor method on the prototype of the Boolean type.

Boolean.prototype.xor = function ( value ) { return !!this !== !!value; };

When we execute the following statement we get an unexpected result.

false.xor(false);   // => true

Why does xor resolves in an unexpected manner?

Because this is not false, this inside the function is the complete object and it evaluates to true when it's converted to true the same way that !!{} is true.
__match_answer_and_solution__


Write the code to fix the implementation of xor method:

Boolean.prototype.xor = function ( value ) { return !!this !== !!value; };
Boolean.prototype.xor = function ( value ) { return !!this.valueOf() !== !!value; };
assert(false.xor(false) === false);