2

In PHP, a class can implement the ArrayAccess interface, so the class can be instantiate and accessed like:

class MyClass implements ArrayAccess {
  protected $data = array();
  public function offsetGet($offset) {
    return isset($this->data[$offset]) ? $data[$offset] : null;
  }
  public function offsetSet($offset, $value) {
    $this->data[$offset] = $value;
  }
  public function __toString()
  {
    return print_r($this->data);
  }
}

$a = new MyClass;
$a['item'] = 10;
$a['item2'] = 'adsf';
echo $a;
// Outputs Array( 'item' => 10, 'item2' => 'adsf' )

Is there anything like this in Javascript? I want to be able to create an iterator class in Javascript, but the items can be accessed by using the bracket notation instead of using get('item')/set('item2', 'asdf') so it can act like an Array instead of an Object, but still having all the functions from my constructor (next, prev, is, reset, etc)

I'm asking this because AngularJS, when using angular.element(), it returns an object (?) like Object [div.id1] in firebug console

3
  • So you want an "array", that can be accessed by key (like an object literal), that still has array methods? What are next, prev, is, reset, etc.? Commented Apr 25, 2013 at 1:32
  • yes. next, prev, is, reset are the class iterator functions, i've added some info why I'm asking for this at the bottom of the question Commented Apr 25, 2013 at 1:35
  • So do you already have a class in Javascript? If so, can you show it? Commented Apr 25, 2013 at 1:38

1 Answer 1

1

The answer is yes, but not in any app that is intended to be used by the general public.

ECMAScript 5 supports 'get' and 'set' declarations (on known properties), and in future revisions will support proxies (which could listen for any changes to an object), but if your application is going to support IE anything, or any devices where users aren't opting into downloading developer browsers, and enabling dev-switches, you're going to have to either leave a lot of users behind for a few years (consider contract-lengths for smartphones, and that mobile browsers still aren't there), or implement the old version and test compatibility inside try-catch clauses, because using these features otherwise could cause hard exceptions, or just not work, depending.

And thus, yes, but you'll have to do it the old way, anyway.

Sign up to request clarification or add additional context in comments.

4 Comments

I see, and AngularJS does this only on good browsers? I haven't tested it in IE yet, but angular.element returns an "array object"
@pocesar That's a very long story. But DOM-mutators aren't the same as JS proxies, angular DOM elements are wrapped with jQuery, because of jQuery's entity-system, which is used to mimic mutators for unsupporting browsers. Try it with jQuery some time: var el = $("#my-el"); - it's not actually a DOM element. el[0]; is the element. You should look further into Angular's implementation, though -- it's really, really well engineered, and is something that makes for some great pet-projects, learning how to implement similar things.
nice, I'll read about the upcoming stuff then :) ES6 looks really promising
@pocesar if you're just looking to access them, using dot/array notation (and not set them), then you can use set to modify attached properties of the object: obj.set = function (name, val) { private_obj[name] = val; this.name = val; }; You can't set properties this way, but you could access them, and modifying with set would still modify the public version. ...hooray for small mercies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.