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);


function add2(a, b) {
	var o = new MyObj(a);
  o.add(b);
  return o;
}

var result2 = add2(1, 2).multiply(3).substract(4);
console.log(result2.value);
console 命令行工具 X clear

                    
>
console