user1905720
user1905720

Reputation:

Are there any good underscore.js alternatives that extend the core js classes?

I find the _.something(somevar, some_function_or_other_thing); "syntax" quite ugly. What are some good alternatives that use ruby-like iterators and similar stuff:

10..times(function(i) {
   console.log(i);
});

uppercasefoobar = ["foo", "bar"].each(function(i) {
    return i.toUpperCase();
});

Also, I am using node.js, so it should focus more on the code than DOM stuff.

Upvotes: 6

Views: 4656

Answers (3)

Tony Cronin
Tony Cronin

Reputation: 1661

Suprised no one mentioned Lo-Dash. Lo-Dash is a superset of Underscore, adding numerous methods. John-David Dalton (creator of Lo-Dash) explains the key differences between the two libraries in answering an SO question here.

edit: found a detailed blog post "Say hello to Lo-Dash", detailing the background to the library and comparisons to other so called 'js utility belts'.

Upvotes: 5

user1905720
user1905720

Reputation:

Found one myself! http://sugarjs.com/

(and yes, I do realize extending native classes essentially forks the language... that is my motive...)

Upvotes: 2

azz
azz

Reputation: 5930

Looks like you're looking for Array.prototype.map

uppercasefoobar = ["foo", "bar"].map(function(i) {
    return i.toUpperCase();
});
// uppercasefoobar == ["FOO", "BAR"]

With underscore.js you can also write:

_.range(10).forEach(function(i) {
    console.log(i);
});

Edit You can also use:

_(3).times(function(i) {
    console.log(i);
});

Not a pretty as ruby syntax, but it gets the job done.

In general, underscore provides an object oriented version of most functions, where:

_.something(variable, params);

Is equivalent to

_(variable).something(params);

Upvotes: 4

Related Questions