I am reading through John Resig's Secrets of Javascript ninja and was trying out one of the examples on currying and parital functions. The code is as follows:
<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>
However, the following code seems to generate an error Uncaught TypeError: Illegal invocation on the apply function.
I cant seem to figure out the reason as it all seems to make sense.
bindClick
will return an anonymous function that calls the function elem.addEventListener
with window
as the function context (this
) and the arguments will be ["click", function() {console.log("OK"); }]