0

I'm trying to use underscore in an OO way:

var myCollection = _([]);
myCollection.push('one');
myCollection.push('two');
myCollection.push('three');

How do I get the item at a numeric index? I'm sure I'm missing something, other than using myCollection.toArray()[1]. There's no myCollection.get(1)?

Also, if I use myCollection.push('something'), it returns an array, which isn't chainable. I'm really confused why there isn't something out there like this already.

As a follow up to this question, I'm trying to use underscore in an OO way, and not have to rewrap the array/object every time. After some of these challenges, it appears its not really meant to be used this way?

So now I'm wondering, is there a better library that has a generic, chainable, OOP, collection wrapper?

Community
  • 1
  • 1
Michael Lewis
  • 4,252
  • 6
  • 28
  • 39
  • Why not just use a "dumb Array" here? Anyway, to answer the last question: jQuery (mostly, the results of `$(..)` is a collection and `x[0]` is the same as `x.get(0)`). – user2864740 Oct 31 '13 at 00:11
  • I'd really like an object I can add methods to... – Michael Lewis Oct 31 '13 at 00:12
  • Plus, I'd like to attach the underscore helper functions to the collection – Michael Lewis Oct 31 '13 at 00:13
  • I have messed around with using jQuery for non-dom collections. It feels too heavy for a simple collection. What I'd really like is something with `.extend` like Backbone's classes. – Michael Lewis Oct 31 '13 at 00:15
  • If you're committed to Underscore, maybe write your own Backbone-like proxies? http://jsfiddle.net/7CRGQ/4/ Otherwise, maybe Lodash would be more to your taste? It seems to chain in the case you mentioned. (I'm sure there are other OO-type JS libraries, too -- I don't have broad knowledge in this area.) – McGarnagle Oct 31 '13 at 00:27
  • Underscore is meant to be used in a functional way, not as an object-orietated wrapper. Why would you need that? – Bergi Oct 31 '13 at 00:27
  • `.push` does never return an array, it does return the length of the array after the element(s) are appended to it. – Bergi Oct 31 '13 at 00:27
  • If you want something chainable, use [chaining with `_.chain()`](http://underscorejs.org/#chaining) instead of the simple wrapper utility `_()` – Bergi Oct 31 '13 at 00:28
  • @Bergi: Underscore is meant to be used both ways, hence the `_.f` and `_().f` interfaces. – mu is too short Oct 31 '13 at 01:14

2 Answers2

3

If you really need that method to access single elements directly on a wrapped array (or object), you can implement it yourself easily using _.mixin:

_.mixin({
    get: function(obj, key) { // works on arrays as well
         return obj[key];
    }
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

If you really want to use Underscore like this, you can simply add necessary method yourself to _.prototype:

// probably name it something other then get
_.prototype.get = function(i) {
    return this.toArray()[i];
}
dfsq
  • 191,768
  • 25
  • 236
  • 258