解壓代碼。如果我們開始與
var addTo = function (passed) {
var add = function(inner){
return passed + inner;
};
return add;
}
var x = addTo(2);
console.log(x(4));
然後讓我們忽略範圍並將其解壓:
function actualAdd(passed, inner){
return passed + inner;
};
function addTo(passed) {
return function(x) { return actualAdd(passed, x);
}
var addSomethingTo2 = addTo(2);
// This is a handle for the actualAdd() function,
// in which "passed" is always the value 2.
console.log(addSomethingTo2);
var thisIsSix = addSomethingTo2(4);
// By calling addSomethingTo2(4) we run the calculation
// 2 + 4 and return the result of that: 6
console.log(thisIsSix);
'AddTo就'返回一個函數,所以如果你控制檯登錄'addTo(4)',你將記錄一個需要被調用的函數本身,然後才能從中獲得有意義的東西。 –
因爲它會變成:'console.log(addTo(2)(4));' – Ultimater