I came across David Walsh's once
function here:
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
// Usage
var canOnlyFireOnce = once(function() {
console.log('Fired!');
});
canOnlyFireOnce(); // "Fired!"
canOnlyFireOnce(); // nada
My question is what is the tracker
variable here? Doesn't result
become null
each time canOnlyFireOnce()
is fired? What is the purpose of setting the fn
to null?