Make object false

Is it possible to override something on an object in JavaScript to make it seem false?

For example, I create an object like this:

function BusyState() { var self = this; self.isSet = false; self.enter = function () { self.isSet = true; }; self.exit = function () { self.isSet = false; }; }; var isLoading = new BusyState(); isLoading.enter(); 

I can check the employment like this:

 if (isLoading.isSet) 

But I would like to write this as a shorthand:

 if (isLoading) 

Can I do something with my object so that it looks true or false depending on the value of isSet ?

+9
javascript
source share
1 answer

As far as I know, this is not possible. If you read the ECMAScript specification , you will see that if(isLoading) evaluates to if(ToBoolean(isLoading) === true) and ToBoolean () always returns true for the object (as you can see in the table in the specification).

+11
source share

All Articles