Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a object's bracket [index] getter/setter in JavaScript?

I am currently building a Doubly linked list implementation.
What I am trying (or hoping) to do, is to use a setter / getter to set elements in the list, just like you would in an array:

var index = 5;
list[index] = node_x;

However, I can't just use this syntax, because the nodes aren't technically properties of the list.
Think of the list as 2 hooks. These 2 hooks are connected to 2 ends of a chain, but you can only access the those 2 connecting chain-links (And their siblings through them).
The rest of the chain-links are not properties of the list. That's why I need to override the implementation of the brackets [] on my object, if possible.

My (simplified / shortened) code is:

(function () {
    "use strict"
    window.List = function () {
        var Length //Etc
        return {
            //Getter / Setter example.
            get length() {return this.Length;},
            set length(n) {this.Length = n;},
            //Function example.
            insertBeginning: function (newNode) {/*  */},
            insertEnd: function (newNode) {/*  */},

            //Index getter / setter attempt.
            get i(index){ console.log(index); },
            set i(index, node){ console.log(index); }
        };
    };

}());

var list = new List();
list.length = 10 //This works just fine
console.log(list.length) // Returns 10, like expected.

Now, what I was trying to do with the i getter/setter, is to set elements like this:

var index = 5;
list.i(index) = node;

But of course, that doesn't work, since:

  1. i is not a function;
  2. I can't assign variables to a function, obviously.

I could of course just use a function to set the elements:

list.setAtIndex(index, node);

But I'd prefer to override the array notation for the object, somehow.

So, my question is, is that possible? And if so, could I get some hints? My search attempts have only returned resources like this, I know how getters / setters work by now.

like image 262
Cerbrus Avatar asked Dec 13 '12 11:12

Cerbrus


2 Answers

I would like to suggest that this is a very bad idea. The cost of getting an item at index i for a linked list is O(n). Accessing a linked list through an index is a mistake Java made that others should not repeat. This mistake was not made for C++ and C#.

Arrays often work better for random insertion and deletion in most cases, because a O(n) cost of linear search completely dominates in terms of performance, and arrays work better for pre-fetching. No, really: http://bulldozer00.com/2012/02/09/vectors-and-lists/

I suggest using a linked list implementation without index access at all for when you can really prove that you'll get a performance benefit out of using a linked list, perhaps for implementing a queue. I suggest using the built in arrays for all other cases. In addition to being better in general for most uses, you'll get the benefit of far more optimisations for the built in arrays than you will for any third party linked list implementation.

like image 163
w0rp Avatar answered Oct 11 '22 08:10

w0rp


Aside from this being a bad idea, it's simply not possible.

like image 38
Cerbrus Avatar answered Oct 11 '22 10:10

Cerbrus