Chaining
Create a chained API
HTML
<div id="result"></div>
JavaScript
var Calc = function(start) {
// Capture this if the caller isn't trusted
var that = this;
this.add = function(x) {
start = start + x;
return that;
};
this.multiply = function(x) {
start = start * x
return that;
};
this.equals = function(callback) {
callback(start);
return that;
};
};
new Calc(0)
.add(1)
.add(2)
.multiply(3)
.equals(function(result) {
$('#result').text(result);
});