_redefine.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. var global = require('./_global')
  2. , hide = require('./_hide')
  3. , has = require('./_has')
  4. , SRC = require('./_uid')('src')
  5. , TO_STRING = 'toString'
  6. , $toString = Function[TO_STRING]
  7. , 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 {
  19. if(!safe){
  20. delete O[key];
  21. hide(O, key, val);
  22. } else {
  23. if(O[key])O[key] = val;
  24. else hide(O, key, val);
  25. }
  26. }
  27. // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative
  28. })(Function.prototype, TO_STRING, function toString(){
  29. return typeof this == 'function' && this[SRC] || $toString.call(this);
  30. });