Jaketr00 Jaketr00 - 2 years ago 73
Javascript Question

Prototype change original

I have a prototype function that changes Boolean

and visa versa.

Boolean.prototype.switch = function() {
return this.toString() === 'false';

And currently I have to use the following to change the original value.

var a = true;
a = a.switch();

Is there a way I can change the original variable (
) without
a =
? Meaning the script below run the same as above?

var a = true;

I am creating a quick game, and in the game there are 25 blocks which can have a value of on or off, or as I made it,
. When you click the block it switches values. While I was making the code to switch the values, I became a bit curious to if there is a way to remove
a =
and still change the value.

Note I am not asking for help with how to make this prototype function, I am asking if there is a way to change the value without a left hand side in an assignment

Answer Source

No, that's not possible - Boolean objects are effectively immutable.

You cannot assign to this in a method, and nor does the Boolean object expose any method to change its own value that you might have called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download