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;
};
};
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.
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.
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 totrue
once
is calledmemo
is returnedWhen you run the returned function again (since
ran
is nowtrue
):memo
is returnedWhat the function does can be found in the documentation:
Why set
func = null
is explained in this commit message: