Nodejs Number Calculate valueOf() return x; };

Here you can find the source of valueOf() return x; };

Method Source Code

function foo(a, b) {
    var result = a + b;
    if (result)//from  w  w w  . j a v a 2  s .c om
        return (a + b) + result + this;
    else
        return this.f;
}

noInline(foo);

var x;
Number.prototype.valueOf = function() { return x; };

function test(this_, a, b, x_) {
    x = x_;
    var result = foo.call(this_, a, b);
    if (result != (a + b) * 2 + x_)
        throw "Error: bad result: " + result;
}

for (var i = 0; i < 100000; ++i)
    test(5, 1, 2, 100);

test(5, 2000000000, 2000000000, 1);
test(5, 1073741774, 1073741774, 1000);

Related

  1. tan()
    Number.prototype.tan = function() {
        return Math.tan(this);
    
  2. testNumber()
    function Number(){
      this.start = 20
      this.testNumber();
      this.answer = 1
    };
    Number.prototype.testNumber = function(){
      var i = 1;
      var start = this.start;
      while(i <= 20){
    ...
    
  3. trim(a, b)
    Number.prototype.trim = function(a, b) {
      var min = Math.min(a, b),
          max = Math.max(a, b);
      return Math.min(Math.max(parseInt(this, 10), min), max);
    };
    
  4. tween(target, position)
    Number.prototype.tween = function(target, position){
      return this + (target-this) * position;
    };
    
  5. twos(n)
    Number.prototype.twos = function(n) {
      if (this >= 0) {return pad(this.toString(2),n)}
      var max = Math.pow(2,n-1);
      return "1" + pad((max + this).toString(2),n-1)
    function pad(num, size) {
        var s = "00000000000000000000" + num;
        return s.substr(s.length-size);
    Number.prototype.twos = function(n) {
      var ret = "";
      while(n)
        ret += ( (this >> --n) & (1) );
      return ret;
    };
    
  6. with_sign()
    Number.prototype.with_sign = function(){
      return ( this > 0 ) ? "+"+this : this.toString();