SOURCE

function add(a, b) {
  var addResult = a + b;
  return {
    multiply: function(n) {
    	var multiplyResult = addResult * n;
    	return {
    		substract: function(n) {
    			return multiplyResult - n;
  			}
			};
		}
	};
}

var result = add(1, 2).multiply(3).substract(4);
console.log(result);


var MyObj = function(value) {
	this.value = value;
};

MyObj.prototype.add = function(n) {
  this.value += n;
  return this;
}

MyObj.prototype.multiply = function(n) {
  this.value *= n;
  return this;
}

MyObj.prototype.substract = function(n) {
  this.value -= n;
  return this;
}

var obj = new MyObj(1);
obj.add(2).multiply(3).substract(4);
console.log(obj);
console.log(obj.value);
console 命令行工具 X clear

                    
>
console