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 8263253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:02:08+00:00 2026-06-08T04:02:08+00:00

I was examining how UnderscoreJS implements their each / forEach //somewhere up top: var

  • 0

I was examining how UnderscoreJS implements their each/forEach

//somewhere up top:
var breaker = {};

//then the each function
var each = _.each = _.forEach = function (obj, iterator, context) {
    if (obj == null) return;
    if (nativeForEach && obj.forEach === nativeForEach) {
        obj.forEach(iterator, context);
    } else if (obj.length === +obj.length) {
        for (var i = 0, l = obj.length; i < l; i++) {
            if (iterator.call(context, obj[i], i, obj) === breaker) return;
        }
    } else {
        for (var key in obj) {
            if (_.has(obj, key)) {
                if (iterator.call(context, obj[key], key, obj) === breaker) return;
            }
        }
    }
};

//iterator = callback
//context  = optional third parameter of each to provide context in the callback
//obj      = the list
//key      = key of the object (i for index when an array)

Basically, it’s executing the callback for each item in the object/array. But this like confuses me

if (iterator.call(context, obj[key], key, obj) === breaker) return;

From what I understand, if the callback returns an object, the loop breaks, but… Why it’s comparing to breaker which is an internal object in the underscore module?. Doesn’t it evaluate to false all the time since, even if the callback does return an object, it is always false since it’s not the same object (therefore the loop never breaks). What’s the reason behind this?

  • 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-08T04:02:10+00:00Added an answer on June 8, 2026 at 4:02 am

    They use each internally for e.g. some. Since some does short-circuit, they can have each break there using the “secret” object, while not exposing this feature for regular users. They don’t expose the break feature because the native function doesn’t do that, either (so their shim is as native-like as possible). If they did, the break feature would only be available if the native function is unavailble, which isn’t particularly helpful.

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

Sidebar

Related Questions

I'm confusing about opening different threads with OpenThread Function and examining them with NtQueryInformationThread
I am examining the private c++ function hooking code snippet from mobilesubstrate and see
Without examining each method, how can I find duplicate methods in a class? Duplicate
I am currently examining different NoSQL and RDBMSes regarding their replication abilities in order
I was examining one of my client's code and came across this. $(document).ready(function() {
I am examining a js file form Bram Jetten: Notification.fn = Notification.prototype; function Notification(value,
Examining Zend Framework, I found that all setter methods (of those I’ve examined) return
Examining bytecode, I've noticed javac seems to duplicate checkcast instructions when casting to array
Examining the compiled code of the sinf() on our GCC implementation, I see that
Our environment: Drupal+MySQL Examining the query log indicates that the following query, originating from

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.