Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

underscore-oop.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var _setup = require('./_setup.js');
  2. var underscore = require('./underscore.js');
  3. var _chainResult = require('./_chainResult.js');
  4. var each = require('./each.js');
  5. // Add all mutator `Array` functions to the wrapper.
  6. each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
  7. var method = _setup.ArrayProto[name];
  8. underscore.prototype[name] = function() {
  9. var obj = this._wrapped;
  10. if (obj != null) {
  11. method.apply(obj, arguments);
  12. if ((name === 'shift' || name === 'splice') && obj.length === 0) {
  13. delete obj[0];
  14. }
  15. }
  16. return _chainResult(this, obj);
  17. };
  18. });
  19. // Add all accessor `Array` functions to the wrapper.
  20. each(['concat', 'join', 'slice'], function(name) {
  21. var method = _setup.ArrayProto[name];
  22. underscore.prototype[name] = function() {
  23. var obj = this._wrapped;
  24. if (obj != null) obj = method.apply(obj, arguments);
  25. return _chainResult(this, obj);
  26. };
  27. });
  28. // Extracts the result from a wrapped and chained object.
  29. underscore.prototype.value = function() {
  30. return this._wrapped;
  31. };
  32. // Provide unwrapping proxies for some methods used in engine operations
  33. // such as arithmetic and JSON stringification.
  34. underscore.prototype.valueOf = underscore.prototype.toJSON = underscore.prototype.value;
  35. underscore.prototype.toString = function() {
  36. return String(this._wrapped);
  37. };
  38. module.exports = underscore;