Search code examples
javascriptc++node.jsoperator-overloadingv8

Operator overloading in V8


This might be a simple question, but I couldn't find anything about this subject on google.

Obviously this isn't possible in pure javascript, but let's say I'm creating some sort of container class in V8 and passing that class back to javascript. Can I implement operator overloading in V8 so that it's possible to access array elements in JS like foo[i], or am I stuck with foo.at(i) or similar method calls?

Thanks!


Solution

  • You can already do this without overloading; you can use bracket notation to access your own object's properties:

    function Foo() {
      for (var i=0; i<arguments.length; i++)
        this[i] = arguments[i]
    }
    
    var foo = new Foo(1,2,3)
    
    foo[1] //=> 2