Last active
November 30, 2016 00:54
-
-
Save terrierscript/7c2b578c7d02200ba6b0279e1b54cc3a to your computer and use it in GitHub Desktop.
babel-polyfillのArrayの処理まわりがやっていること
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 元のこんなコードを読み解く | |
| // | |
| // let DEFINE_PROPERTY = "defineProperty"; | |
| // function define(O, key, value) { | |
| // O[key] || Object[DEFINE_PROPERTY](O, key, { | |
| // writable: true, | |
| // configurable: true, | |
| // value: value | |
| // }); | |
| // } | |
| // | |
| // "pop,reverse,shift,keys,values,entries,indexOf,every,some,forEach,map,filter,find,findIndex,includes,join,slice,concat,push,splice,unshift,sort,lastIndexOf,reduce,reduceRight,copyWithin,fill".split(",").forEach(function(key) { | |
| // [][key] && define(Array, key, Function.call.bind([][key])); | |
| // }); | |
| console.log([].pop) // [Function: pop] | |
| console.log(Array.pop) // undefined | |
| /////////////// | |
| const target = [] | |
| if(!target["pop"]){ | |
| return | |
| } | |
| if(Array["pop"]){ | |
| return | |
| } | |
| Object.defineProperty(Array, "pop", { | |
| writable: true, | |
| configurable: true, | |
| value: Function.call.bind(target["pop"]) | |
| }); | |
| /////////////// | |
| console.log([].pop) // [Function: pop] | |
| console.log(Array.pop) // [Function: bound call] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment