Chaining functions
by toubia95
CSS
/* with or without prototypes ??? */
JavaScript
/*
function MyNumber(n) {
var internal = Number(n);
this.one = function() {
internal += 1;
return this;
}
// this.two analogous
this.two = function() {
internal += 2;
return this;
}
this.valueOf = function() {
return internal;
}
}
*/
//console.log(new MyNumber(5).one().two().two().valueOf()); // 10
//console.log(new MyNumber(5)); // 10
/* Base */
var Add = function(a,b) {
return a+b;
}
var By = function(a,c) {
return a*c
}
Add(5,6); // 11
By(Add(5,6),2); // 22
Add(5,6).By(2); // 22
var Add = function(a,b) {
return x = a+b;
}
var By = function(c) {
return x*c
}
Add(5,6); // 11
Add(5,6).By(2); // 22
/* Chained */
var cAdd = function(a,b) {
this = a+b;
return this;
}
var cByTwo = function(a) {
return a*2
}
console.log(cAdd(5,6));