Last active
May 9, 2016 22:26
-
-
Save zwhitchcox/efbedb7035d086ffc02ada0ebe30bc84 to your computer and use it in GitHub Desktop.
Extendable Array (Implemented in ES6)
This file contains 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 passThroughs = ['toString', 'toLocaleString', 'unshift'] | |
function FunArr(arr) { | |
let fun = { | |
i: arr | |
} | |
for (let proxy of Object.getOwnPropertyNames(Array.prototype)) { | |
Object.defineProperty(fun, proxy, { | |
get: () => typeof fun.i[proxy] === 'function' ? | |
passThroughs.includes(proxy) ? | |
(...args) => fun.i[proxy].apply(fun.i,args) : | |
(...args) => FunArr(fun.i[proxy].apply(fun.i,args)) : | |
fun.i[proxy], | |
configurable: true, | |
enumerable: false, | |
}) | |
} | |
// your array functions go here | |
fun.log = () => ((console.log(fun.i), fun)) | |
return fun | |
} | |
let x = FunArr([1,2,3]) | |
console.log(x.length) | |
console.log(x.i[0]) | |
x.log() | |
x | |
.filter((e)=>true) | |
.log() // [1,2,3] | |
.filter((e)=>true) | |
console.log(x.toString()) // 1,2,3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment