Thursday 19 June 2014

JavaScript revocable function


function revocable(f) {
return {
invoke: function () {
return f.apply(this, arguments);
},
revoke : function () {
f = null;
}
};
}

var temp = revocable(console.log);
temp.invoke('1'); // logs
temp.revoke();
temp.invoke('2'); // throws

// Works in FF, apparently Chrome has a bug... hmmm better ways to do this i think
// use with timeout to allow access
// to a fuction for a set period of time

JS Fiddle:  http://jsfiddle.net/stevenhollidge/tKC5Q/

No comments:

Post a Comment