0

How can I make my object acts like an array (but not only) ?

Imagine I create a class Rect which has one attribute : this.rect (an array [x, y, w, h]). And some get / set attributes : this.top returns this.rect[y]. How can I do this : r = new Rect (1, 2, 3, 4); console.log(r) => [1, 2, 3, 4] console.log(r[0]) => 1 console.log(r.top) => 2 Hope you understood (I am french). Thank.

31st Mar 2018, 9:24 AM
clement
clement - avatar
5 Answers
+ 3
it is an array not object
31st Mar 2018, 9:48 AM
Amethyst Animion
Amethyst Animion - avatar
31st Mar 2018, 10:45 PM
Amethyst Animion
Amethyst Animion - avatar
0
It has to be an object for r.top
31st Mar 2018, 10:19 AM
clement
clement - avatar
0
UP šŸ–
31st Mar 2018, 8:59 PM
clement
clement - avatar
0
I think you didn't understand. I want my obect to ALSO acts like an array. For exemple in Python with the special method __getitem__.
1st Apr 2018, 9:16 AM
clement
clement - avatar