what is it used for _.once in underscore?

2019-04-27 20:08发布

问题:

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;
    };
  };

回答1:

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.



回答2:

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



回答3:

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:

  1. ran is set to true
  2. the function passed into once is called
  3. the function reference is deleted (presumably to aid garbage collection)
  4. memo is returned

When you run the returned function again (since ran is now true):

  1. memo is returned


回答4:

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.