underscore-array-methods.js 980 B

123456789101112131415161718192021222324252627282930
  1. define(['./underscore', './each', './_setup', './_chainResult'], function (underscore, each, _setup, _chainResult) {
  2. // Add all mutator `Array` functions to the wrapper.
  3. each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
  4. var method = _setup.ArrayProto[name];
  5. underscore.prototype[name] = function() {
  6. var obj = this._wrapped;
  7. if (obj != null) {
  8. method.apply(obj, arguments);
  9. if ((name === 'shift' || name === 'splice') && obj.length === 0) {
  10. delete obj[0];
  11. }
  12. }
  13. return _chainResult(this, obj);
  14. };
  15. });
  16. // Add all accessor `Array` functions to the wrapper.
  17. each(['concat', 'join', 'slice'], function(name) {
  18. var method = _setup.ArrayProto[name];
  19. underscore.prototype[name] = function() {
  20. var obj = this._wrapped;
  21. if (obj != null) obj = method.apply(obj, arguments);
  22. return _chainResult(this, obj);
  23. };
  24. });
  25. return underscore;
  26. });