How would you overload the [] operator in javascript

slyprid picture slyprid · Nov 10, 2009 · Viewed 38.8k times · Source

I can't seem to find the way to overload the [] operator in javascript. Anyone out there know?

I was thinking on the lines of ...

MyClass.operator.lookup(index)
{
     return myArray[index];
}

or am I not looking at the right things.

Answer

Peter Bailey picture Peter Bailey · Nov 10, 2009

You can't overload operators in JavaScript.

It was proposed for ECMAScript 4 but rejected.

I don't think you'll see it anytime soon.