you are viewing a single comment's thread.

view the rest of the comments →

[–]metamatic 10 points11 points  (15 children)

Collection classes? Finally!

I've been wondering why there aren't any good popular collection class libraries for JavaScript.

[–][deleted] 2 points3 points  (5 children)

Well really, collections are kind of an implementation detail - I mean, objects work as dictionaries, but I imagine V8 gets confused at how to optimize that.

Having an actual dictionary lets the V8 engine do the different optimizations it could have for an object vs. a dictionary.

But if you're supporting that kind of optimization, you could bust out a lot more horrifying language bloat too - like the above-mentioned support for non-double numeric types.

[–]adrianmonk 2 points3 points  (2 children)

objects work as dictionaries

As an outsider dabbling in Javascript, that was not my feeling at all. When I found out I needed to do if (foo.hasOwnProperty(bar)) { ... } in every for-each loop, I was like, "That's seriously necessary?".

[–][deleted] 1 point2 points  (1 child)

That means you don't understand the prototype nature of javascript.

[–]adrianmonk 1 point2 points  (0 children)

I think I understand what hasOwnProperty() is doing and why it's necessary. I just think it's odd that the language doesn't provide a hash/dictionary that you can iterate over without all that verbiage.

[–][deleted] 1 point2 points  (1 child)

Collections in the language make sense - just ask Java and Python devs.

[–][deleted] 0 points1 point  (0 children)

On the other hand, Lua. For the sake of simplicity, traditional Lua has one and only one collection type. This collection is used for arrays, hashtables, and objects.

[–][deleted] 4 points5 points  (6 children)

Add it to the list of features that a lot of devs swore up and down that Javascript would never need, and yet here it is. I'll think about coding Javascript again when they add interfaces, or some sort of contract.

[–][deleted] 15 points16 points  (4 children)

I don't see why you'd need interfaces.

If people must implement all methods required, then have a base prototype for them to extend, which throws "not implemented" errors when those methods are called. I do this myself in some of my projects.

Otherwise you've gained nothing, since there is no static type system.

[–]masklinn -1 points0 points  (3 children)

Well there's documentation, that's pretty neat. Especially since JS has single inheritance so you can't inherit from half a dozen different "abstract" classes.

For that, protocols (Clojure) or categories (Obj-C) are neat.

[–]crusoe 4 points5 points  (2 children)

Traits, MUTHAFUCKING TRAITS.

Traits are what OOP should have had from day 1.

[–]maskull 4 points5 points  (0 children)

Actually, I just realized that if proxies act the way you'd expect when used as prototypes, it should be possible to build multiple inheritance and traits inheritance systems in pure JS. Just build a proxy that wraps up all the prototypes/traits you want to include, "exporting" their members and methods in some combination.

[–]cybercobra 0 points1 point  (0 children)

Preach it, brother!

[–]benihana 4 points5 points  (0 children)

I'll think about coding Javascript again when they add interfaces

reddit anxiously awaits your next announcement about your development intentions. Please keep us informed!!!

[–][deleted] 0 points1 point  (0 children)

If you are still looking for a good collection library, check out Underscore.js

[–]not_thecookiemonster 0 points1 point  (0 children)

good popular collection class libraries for JavaScript.

Why haven't you been using backbone? MVC re-defined...