I just look at the once API of source in underscore.js, then wandering what is it the used for
in the method, it seems doing nothing:
func = null
the source:
_.once = function(func) {
var ran = false, memo;
return function() {
if (ran) return memo;
ran = true;
memo = func.apply(this, arguments);
func = null;
return memo;
};
};
What the function does can be found in the documentation:
Creates a version of the function that can only be called one time. Repeated calls to the modified function will have no effect, returning the value from the original call. Useful for initialization functions, instead of having to set a boolean flag and then check it later.
Why set func = null
is explained in this commit message:
Assuming we'll never run the wrapped function again on _.once(), we can
assign null to the func
variable, so function (and all its inherited
scopes) may be collected by GC if needed.
From the official underscorejs website:
once _.once(function)
Creates a version of the function that can only be called one time. Repeated calls to the modified function will have no effect, returning the value from the original call. Useful for initialization functions, instead of having to set a boolean flag and then check it later.
var initialize = _.once(createApplication);
initialize();
initialize();
// Application is only created once.
http://underscorejs.org/#once
It's unclear if you are asking about the entire function or just the func = null
line. If the latter, see just step 3, below.
ran
is initially false.
When you run the returned function for the first time:
ran
is set to true
- the function passed into
once
is called
- the function reference is deleted (presumably to aid garbage collection)
memo
is returned
When you run the returned function again (since ran
is now true
):
memo
is returned
Also worth pointing out is that the memo
will hold the result of the initially executed function.
So when you call your function again, it will not be executed but the result of the first call will be returned.