Skip to content

Instantly share code, notes, and snippets.

@artlung
Created July 11, 2010 23:14
Show Gist options
  • Save artlung/471932 to your computer and use it in GitHub Desktop.
Save artlung/471932 to your computer and use it in GitHub Desktop.
Math function that is self-reflective
<script src="exp.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
// alert(ALL.plus(1).minus(5).dividedBy(2).times(1000));
alert(ALL.equals(2).toThePowerOf(2).dividedBy(2).plus(100).plus(234).times(3).dividedBy(5));
alert(ALL.getLog());
// alert(typeof ALL.getLog)
</script>
var ALL = {
value: 0,
log: [],
_log: function(op,n) {
this.log.push(op + ' ' + n + ' = ' + this.value);
},
_numberize: function(n) {
return parseFloat(n);
},
equals: function(n) {
this.value = this._numberize(n);
this._log('equals',n);
return this;
},
plus: function(n) {
this.value+=this._numberize(n);
this._log('plus',n);
return this;
},
minus: function(n) {
this.value-=this._numberize(n);
this._log('minus',n);
return this;
},
dividedBy: function(n) {
n = this._numberize(n);
this.value = (n == 0) ? 'infinity' : this.value /= n;
this._log('dividedBy',n);
return this;
},
times: function(n) {
this.value*=this._numberize(n);
this._log('times',n);
return this;
},
toThePowerOf: function(n) {
this.value = Math.pow(this.value, this._numberize(n));
this._log('toThePowerOf',n);
return this;
},
toString: function() {
return ''+this.value;
},
getLog: function() {
return this.log.join('\n');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment