Skip to content

Instantly share code, notes, and snippets.

@Prosen-Ghosh
Created August 29, 2017 12:24
Show Gist options
  • Save Prosen-Ghosh/bfebcfbc217505ea224119aaa971ec03 to your computer and use it in GitHub Desktop.
Save Prosen-Ghosh/bfebcfbc217505ea224119aaa971ec03 to your computer and use it in GitHub Desktop.
Method Chaining
var calculator = function(){
return this;
}
calculator.prototype.set = function (number){
if(number === undefined)this.number = Math.floor(Math.random() * 1000);
else this.number = number;
return this;
}
calculator.prototype.add = function(number = 0){
this.number += number;
return this;
}
calculator.prototype.sub = function(number = 0){
this.number-= number;
return this;
}
calculator.prototype.div = function(number = 1){
this.number/= number;
return this;
}
calculator.prototype.multi = function(number = 1){
this.number*= number;
return this;
}
calculator.prototype.mod = function(number){
if(number !== undefined)this.number%= number;
return this;
}
calculator.prototype.get = function(){
return this.number;
}
calculator.prototype.inc = function(number){
if(number === undefined)this.number++;
else this.number +=number;
return this;
}
calculator.prototype.dec = function(number){
if(number === undefined)this.number--;
else this.number -=number;
return this;
}
console.log(new calculator().set(10).add(10).sub(2).multi(10).div(2).mod(4).inc().inc(2).dec(3).dec().get());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment