0

Is there a way to define a method on an object / class in JavaScript that acts like toString just for booleans? Example (es6):

class MyClass {
  toBoolean() {
    return this.foo === bar;
  }
}

so I can do something like this

const myClass = new MyClass();
if (myClass === true) baz();

sounds crazy to me, but I'm gonna ask nonetheless.

Lukas
  • 9,752
  • 15
  • 76
  • 120
  • It doesnt even work like that for strings – Rhumborl May 19 '16 at 11:29
  • 2
    Type coercion doesn't happen when using `===` – Patrick Evans May 19 '16 at 11:32
  • 4
    You can't do stuff like overloading the cast operator in C++. `toString` is just a bit special because many functions implicitly try calling `toString` on your objects, but `===` doesn't do anything like that. So there is no way to magically convert it to a boolean for comparison like this. One would still have to use `if(myClass.toBoolean() === true)`, and then it would make more sense to name the method by what it actually does, e.g. `if(myClass.isValid())` :P – CherryDT May 19 '16 at 11:32
  • use `return !!input_string` and add it to Objects prototype – murli2308 May 19 '16 at 11:33
  • @Thilo It's not very well researched, especially the stuff about when `toString` is used, etc., so I didn't think it was worth being an answer. But I'll make it an answer now... – CherryDT May 19 '16 at 11:42
  • Related: http://stackoverflow.com/questions/2081195/can-an-object-be-false – Thilo May 19 '16 at 11:49
  • It really was a play of thought and a general question i had. I did not think through my example with `===` before I posted it. – Lukas May 19 '16 at 11:50
  • 1
    Note that even `new Boolean(false)` is considered `true`. – Thilo May 19 '16 at 11:53
  • 1
    Note that even if you used loose comparison (`==`), whenever you *compare* a Boolean to a different data type, you will end up comparing numbers, since the Boolean value is converted to a number first. – Felix Kling May 19 '16 at 13:48

3 Answers3

4

Apart from the fact that it would be a can of worms as drewmoore pointed out, it's not possible to do it in JavaScript at all.

There is no feature to "hook type conversion" in a way like overloading the cast operator in C++.

The reason why toString appears to do something similar is just because many functions implicitly try calling toString on your objects, and the JavaScript interpreter also does this when you try to concatenate something to a string using + but === doesn't do anything like that - in fact, the "selling point" of === is that it does no type conversion.

So there is no way to magically convert it to a boolean for comparison like this. One would still have to use if(myClass.toBoolean() === true), and then it would make more sense to name the method by what it actually does, e.g. if(myClass.isValid()) or whatever.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
1

With all due respect, even if it were possible it'd be an awful idea:

const myClass = new MyClass();

myClass.foo = true;
if (myClass === true) baz();  //true
if (myClass) foobaz(); //true, as expected, since myClass === true

myClass.foo = false; 
if (myClass === true) baz(); // false
if (myClass) foobaz(); //true - wtf?
drew moore
  • 31,565
  • 17
  • 75
  • 112
-1

This will solve your problem it will convert any type to Boolean.

function Fun (){

}
Fun.prototype.toBoolean = function(a) {
  return !!a;
}

var obj = new Fun("str");
obj.toBoolean("str");
obj.toBoolean(1);
obj.toBoolean({});
obj.toBoolean(0);
murli2308
  • 2,976
  • 4
  • 26
  • 47