Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:59:12+00:00 2026-06-01T07:59:12+00:00

Underscore.js has two ways of calling functions, which I will refer to as object-style

  • 0

Underscore.js has two ways of calling functions, which I will refer to as object-style and function-style. Object-style looks like the following:

_(myObject).each(function (val, key) {
  console.log(key, val);
});

Function-style, on the other hand, looks like this:

_.each(myObject, function (val, key) {
  console.log(key, val);
});

I was happily using object-style calls in my code, but at some point, however, the object style of call disappeared from the underscore.js documentation (though object-style calls still work perfectly fine). I’ve also seen hints around the place (like in the backbone.js documentation) that the function-style is ‘better’ or ‘preferred’.

So, is the function-style of call the preferred method? And if so, can someone explain the reasoning behind this?

Update: @ggozad has partially answered my question. But it seems my understanding of how underscore.js works was formed way back around version 0.4.2. Reading through the change history for underscore.js, you can see this entry for version 1.2.4:

You now can (and probably should) write _.chain(list) instead of _(list).chain().

I would like to know why you should write _.chain(list) instead of _(list).chain().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:59:14+00:00Added an answer on June 1, 2026 at 7:59 am

    The answer by @ggozad is actually very misleading. The object-oriented style has nothing to do with chaining. The example given:

    _([1,2,3]).map(function (item) { return item * 2; }).map(function (item) { return item*3;});
    

    is actually not using underscore chaining at all! It only works because the built-in JS array object has it’s own map() function. Try a function that’s not built-in (like shuffle) and you’ll see it breaks:

    _([1,2,3]).shuffle().shuffle();
    

    The only way to get underscore chaining is to call chain(), which you can do using either style (OO or functional).

    As for why the documentation says you should use _.chain, I’m guessing it’s just a style preference. I’ve opened an issue at GitHub for clarification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem when I enter any input which has underscore (_) data will store to
I have a SQL SELECT query which has a LIKE clause containing an underscore,
How do I escape the underscore character? I am writing something like the following
jQuery's extend function has the option of recursive extension, which I use when creating
I've built a CMS for our company which has a huge number of functions,
I have a model called Metadatum (silly, I know) which has two fields, key
Looking for a function in underscore.js that will take 2 arrays and return a
In underscore.js the for each method starts with the following: var each = _.each
What does the underscore mean in the following regex? [a-zA-Z0-9_] The _ seems to
This is a two part question. The first part is converting the function below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.