If that's the real question you can do it using valueOf:
var Num = function() {
this.values = [100, 200, 300];
this.index = -1;
};
Num.prototype.valueOf = function() {
this.index++;
return this.values[this.index];
};
var num = new Num();
if (num == 100 && num == 200 && num == 300) {
console.log('it works!');
} else {
console.log('it doesnt');
}
It may look like num is 100, 200 and 300 at once but the if statement has 3 expressions: num == 100
then num == 200
and then num == 300
. Each time you compare num to a number with type coercion (using ==
instead of ===
) the valueOf
will be invoked, that changes inner state of the num object instance so next time you compare it'll produce a different value.
I would not use type coercion comparing ==
and instead use ===
, when you use a linter it will usually complain when using ==
as it can cause some unexpected behavior and makes your code less predictable.