javascript - [] operator overload (this[index] operator overload)

JavaScript
[Edit]
+
0
-
0

JavaScript - [] operator overload (this[index] operator overload)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
function SomeClass() { const array = ['a', 'b', 'c']; // this.method1 = function() { }; // this.method2 = function() { }; // this.method3 = function() { }; return new Proxy(this, { get: function(target, name) { return (name in target ? target[name] : array[name]); }, set: function(target, name, value) { // Remove it to disable setter ... if (name in target) { // Remove it to disable setter ... target[name] = value; // Remove it to disable setter ... } else { // Remove it to disable setter ... array[name] = value; // Remove it to disable setter ... } // Remove it to disable setter ... } // Remove it to disable setter ... }); }; // Usage example: const object = new SomeClass(); console.log(object[0]); // a console.log(object[1]); // b console.log(object[2]); // c // object.method1(); // object.method2(); // object.method3();
Reset