Use the following process:
- Create a variable
- Assign an anonymous function to the variable
- Invoke it with the variable reference
- The anonymous function references itself using the variable name
Use it as such:
var foo = function(e)
{
"use strict";
console.log(e);
document.removeEventListener('keyup', foo, false);
}
document.addEventListener('keyup', foo);
You can solve this problem easily using the y
combinator:
function y(f) {
return function () {
return f.bind(null, y(f)).apply(this, arguments);
};
}
Now you can rewrite your code as follows:
document.addEventListener("keyup", y(function (callee, e) {
player.makeGuess(String.fromCharCode(e.keyCode).toLowerCase());
if (player.win_status || player.lose_status) document
.removeEventListener("keyup", callee);
}));
That's all folks.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…