Last active
February 28, 2024 20:54
-
-
Save wentout/90355af3513d7bb319eb2e403ed6b481 to your computer and use it in GitHub Desktop.
Array Dot Notation JS Example
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
'use strict'; | |
const ogp = Object.getPrototypeOf; | |
const osp = Object.setPrototypeOf; | |
const MyArrayConstructor = function() {}; | |
osp(MyArrayConstructor.prototype, new Array); | |
class MyArray extends MyArrayConstructor { | |
constructor(...args) { | |
super(...args); | |
const pre = ogp(this); | |
pre.push(...args); | |
osp(this, new Proxy(pre, { | |
get(target, prop) { | |
prop = prop.replace('_', ''); | |
return pre[prop]; | |
} | |
})); | |
} | |
} | |
const myArray = new MyArray(1, 2, 3); | |
console.log(myArray._0); |
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
'use strict'; | |
class MyArray { | |
constructor(...args) { | |
const pre = new Array(...args); | |
Object.setPrototypeOf(this, new Proxy(pre, { | |
get(target, prop) { | |
prop = prop.replace('_', ''); | |
return pre[prop]; | |
} | |
})); | |
} | |
} | |
const myArray = new MyArray(1, 2, 3); | |
console.log(myArray._0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment