once.js 650 B

123456789101112131415161718192021222324
  1. var before = require('./before');
  2. /**
  3. * Creates a function that is restricted to invoking `func` once. Repeat calls
  4. * to the function return the value of the first call. The `func` is invoked
  5. * with the `this` binding and arguments of the created function.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @category Function
  10. * @param {Function} func The function to restrict.
  11. * @returns {Function} Returns the new restricted function.
  12. * @example
  13. *
  14. * var initialize = _.once(createApplication);
  15. * initialize();
  16. * initialize();
  17. * // `initialize` invokes `createApplication` once
  18. */
  19. function once(func) {
  20. return before(2, func);
  21. }
  22. module.exports = once;