Nodejs Number Add add(a)

Here you can find the source of add(a)

Method Source Code

Number.prototype.add = function(a) {
   return this.valueOf() + a;
};

Number.prototype.subtract = function(a) {
   return this.valueOf() - a;
};

Number.prototype.multiply = function(a) {
   return this.valueOf() * a;
};

Number.prototype.divide = function(a) {
   return this.valueOf() / a;
};

Number.prototype.square = function() {
   return this.valueOf() * this.valueOf();
};

console.log((0).add(2) === 2);/*from   w  w w  . jav  a 2s. c om*/
console.log((5).subtract(1) === 4);
console.log((3).multiply(2) === 6);
console.log((10).divide(5) === 2);
console.log((5).square() === 25);

Related

  1. add()
    Number.prototype.add = function () {
        return this + [].slice.apply(arguments).reduce(function (a, b) {
                "use strict";
                return a + b;
            },0);
    
  2. add(arg)
    Number.prototype.add = function(arg){
      var r1, r2, m;
      try{r1 = this.toString().split(".")[1].length}catch(e){r1=0}
      try{r2 = arg.toString().split(".")[1].length}catch(e){r2=0}
      m = Math.pow(10, Math.max(r1, r2))
      return (this*m+arg*m)/m
    Number.prototype.sub = function(arg){
      return this.add(-arg)
    ...
    
  3. add(n)
    Number.prototype.add = function(n){
      return this+n
    Number.prototype.subtract = function(n){
      return this-n
    Number.prototype.multiply = function(n){
      return this*n
    Number.prototype.divide = function(n){
      return this/n
    Number.prototype.square = function(n){
      var _ = this;
      return Math.pow(_,2)
    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }
    
  4. add(n) return this+n }
    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }
    
  5. add(num)
    Number.prototype.add = function(num) {
      return this + num;
    var n = 5;
    assert(n.add(3) == 8, 'It works when the number is in a variable.');
    assert((5).add(3) == 8, 'Also works if a number is wrapped in parentheses.');
    assert(5.add(3) == 8, 'What about a simple literal?');