_redefine.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. var global = require('./_global');
  2. var hide = require('./_hide');
  3. var has = require('./_has');
  4. var SRC = require('./_uid')('src');
  5. var $toString = require('./_function-to-string');
  6. var TO_STRING = 'toString';
  7. var TPL = ('' + $toString).split(TO_STRING);
  8. require('./_core').inspectSource = function (it) {
  9. return $toString.call(it);
  10. };
  11. (module.exports = function (O, key, val, safe) {
  12. var isFunction = typeof val == 'function';
  13. if (isFunction) has(val, 'name') || hide(val, 'name', key);
  14. if (O[key] === val) return;
  15. if (isFunction) has(val, SRC) || hide(val, SRC, O[key] ? '' + O[key] : TPL.join(String(key)));
  16. if (O === global) {
  17. O[key] = val;
  18. } else if (!safe) {
  19. delete O[key];
  20. hide(O, key, val);
  21. } else if (O[key]) {
  22. O[key] = val;
  23. } else {
  24. hide(O, key, val);
  25. }
  26. // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative
  27. })(Function.prototype, TO_STRING, function toString() {
  28. return typeof this == 'function' && this[SRC] || $toString.call(this);
  29. });