Guest User

Untitled

a guest
Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. // 本来の実装
  2. console.assert([].slice === Array.prototype.slice);
  3.  
  4. const f = () => { /* dummy */ };
  5. Array.prototype.slice = f;
  6.  
  7. // 置き換えられている!
  8. console.assert([].slice === f);
  9.  
  10. const f = () => { return [] };
  11. console.log(f.toString()); // "() => { return [] }"
  12.  
  13. const g = Array.prototype.slice;
  14. console.log(g.toString()); // "function slice() { [native code] }"
  15.  
  16. // 関数が組み込みであるかを判定する関数
  17. const __fpts = Function.prototype.toString;
  18. const isBuiltInFunction = f => typeof f === 'function' &&
  19. __fpts.call(f).slice(-15).startsWith('[native code]');
  20.  
  21. // 例
  22. console.assert(isBuiltInFunction(__fpts)); // it must be true
  23. console.assert(isBuiltInFunction(isBuiltInFunction) === false);
  24. console.assert(isBuiltInFunction(function () {}) === false);
  25. console.log(isBuiltInFunction([].concat)); // (maybe) true
  26.  
  27. // 組み込み関数を「変更」
  28. Array.prototype.slice = Array.prototype.concat;
  29.  
  30. console.assert(isBuiltInFunction(Array.prototype.slice));
  31. // Array.prototype.concatは組み込み関数である
Add Comment
Please, Sign In to add comment