Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 本来の実装
- console.assert([].slice === Array.prototype.slice);
- const f = () => { /* dummy */ };
- Array.prototype.slice = f;
- // 置き換えられている!
- console.assert([].slice === f);
- const f = () => { return [] };
- console.log(f.toString()); // "() => { return [] }"
- const g = Array.prototype.slice;
- console.log(g.toString()); // "function slice() { [native code] }"
- // 関数が組み込みであるかを判定する関数
- const __fpts = Function.prototype.toString;
- const isBuiltInFunction = f => typeof f === 'function' &&
- __fpts.call(f).slice(-15).startsWith('[native code]');
- // 例
- console.assert(isBuiltInFunction(__fpts)); // it must be true
- console.assert(isBuiltInFunction(isBuiltInFunction) === false);
- console.assert(isBuiltInFunction(function () {}) === false);
- console.log(isBuiltInFunction([].concat)); // (maybe) true
- // 組み込み関数を「変更」
- Array.prototype.slice = Array.prototype.concat;
- console.assert(isBuiltInFunction(Array.prototype.slice));
- // Array.prototype.concatは組み込み関数である
Add Comment
Please, Sign In to add comment