Skip to content

Instantly share code, notes, and snippets.

@terrierscript
Last active November 30, 2016 00:54
Show Gist options
  • Select an option

  • Save terrierscript/7c2b578c7d02200ba6b0279e1b54cc3a to your computer and use it in GitHub Desktop.

Select an option

Save terrierscript/7c2b578c7d02200ba6b0279e1b54cc3a to your computer and use it in GitHub Desktop.
babel-polyfillのArrayの処理まわりがやっていること
// 元のこんなコードを読み解く
//
// 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