解压代码。如果我们开始与
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