我正在閱讀John Resig的Javascript忍者祕訣,並嘗試使用其中一個關於咖喱和pariting功能的示例。代碼如下:javascript and currying
<html>
<body>
<button id="test">Click Me!</button>
</body>
<script type="text/javascript">
Function.prototype.curry = function() {
var fn = this,
args = Array.prototype.slice.call(arguments);
return function() {
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments)));
};
};
var elem = document.getElementById("test");
var bindClick = elem.addEventListener.curry("click");
bindClick(function(){ console.log("OK"); });
</script>
</html>
但是,下面的代碼似乎產生錯誤遺漏的類型錯誤:在應用函數調用非法。
我似乎無法找出原因,因爲它似乎都有道理。 bindClick
將返回與window
作爲函數上下文(this
)調用函數elem.addEventListener
匿名函數和變量將是["click", function() {console.log("OK"); }]
其實,[這不是曲線/schönfinkeling,但部分應用](http://en.wikipedia.org/wiki/Currying#Contrast_with_partial_function_application) – Bergi 2013-03-22 09:54:04
hmm。我讀了一些關於差異的文章。但我仍然有點困惑。部分應用程序返回一個函數,其功能與您傳入的函數相似,但其中的參數較少,其他參數已「綁定」。所以,這確實是部分應用。不過,我不明白爲什麼它不會捲曲?好像John Resig在「JS忍者的祕密」和Douglas Crawford的「JS:The Good parts」中定義了相同的功能並將其命名爲「咖喱」。 – henrybai 2013-03-22 10:15:38