Created
August 29, 2017 12:24
-
-
Save Prosen-Ghosh/bfebcfbc217505ea224119aaa971ec03 to your computer and use it in GitHub Desktop.
Method Chaining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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