Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active February 28, 2024 20:54
Show Gist options
  • Save wentout/90355af3513d7bb319eb2e403ed6b481 to your computer and use it in GitHub Desktop.
Save wentout/90355af3513d7bb319eb2e403ed6b481 to your computer and use it in GitHub Desktop.
Array Dot Notation JS Example
'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);
'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