Less than 10

I am working on calling random code and I cannot figure out how much this is possible.

function(obj) {
   if ( (obj < 10) && (obj > 10) ) {
     return true;
   }
}

Things I tried set the interval to change the variable to 0ms (which ends with the default browser), making obj a life function that increments a global variable each time, and a whole bunch of other seemingly less useful approaches. Any ideas here, or pointers to something obvious, am I missing?

+4
source share
1 answer

The key is in the variable name "obj". When objects are compared, the method is called valueOf(). If we provide a method valueOfthat returns a different value each time:

function test(obj) {
   if ( (obj < 10) && (obj > 10) ) {
     return true;
   }
}

var Obj = function() {
  var flag = false;
  
  this.valueOf = function() {
    if( flag ) {
      return 11;
    }

    flag = true;
    return 9;
  }
}

console.log( test( new Obj() ) );
Run codeHide result

toValue 9 (9 < 10) 11 (11 > 10).

+14

Source: https://habr.com/ru/post/1659287/


All Articles