2 回答

TA貢獻1839條經(jīng)驗 獲得超15個贊
myCategorize不是柯里化函數(shù),它是實現(xiàn)模塊模式的 IIFE。最后一行末尾的括號執(zhí)行頂級function(z):
var myCategorize = function(z) {
return function(q) {
return 1;
}
}('window'.indexOf('w') > 0 || window || false);
//^--------------------------------------------^
并傳入 的值z。如果我重新編寫它以添加額外的括號和名稱位會更清楚:
var myCategorize = (function outer(z) {
//start of outer --^
return function inner(q) {
return 1;
}
})('window'.indexOf('w') > 0 || window || false);
//^-- end of outer
所以你分配給myCategorize的只是inner. 要執(zhí)行它,只需添加括號和一個參數(shù):
var myCategorize = function(z) {
return function(q) {
return 1;
}
}('window'.indexOf('w') > 0 || window || false);
console.log("myCategorize", myCategorize);
var executionResult = myCategorize("foo");
console.log("executionResult", executionResult);

TA貢獻1856條經(jīng)驗 獲得超5個贊
添加括號時它已經(jīng)調(diào)用了一次函數(shù),所以它的值為myCategorize:
? (q) {
return 1;
}
然后你調(diào)用該函數(shù),let category = myCategorize(bookTitles[i])其值為category:1
是這樣,則等效的let category = myCategorize(bookTitles[i])(categories);就是
let category = 1(categories); 拋出那個錯誤
var myCategorize = function(z) {
return function(q) {
return 1;
}
}('window'.indexOf('w') > 0 || window || false);
let category = myCategorize('x'); // 1
console.log(category);
let category2 = myCategorize('x')('y'); // throws, equovalent to 1('y');
添加回答
舉報