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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:53:41+00:00 2026-06-07T06:53:41+00:00

I found myself in an interesting situation. I am using an object literal to

  • 0

I found myself in an interesting situation. I am using an object literal to represent a product in the real-world. Now each product has a length associated to it for shipping purposes. It looks something like this:

var product = {
  name: 'MacBook Pro 15 Inch',
  description: 'The new macbook pros....',
  length: 15
  height: 15
  Weight: 4
}

This this works fine. But for products that have unknown length they default to length -1.

Again this works fine, until you try to do this:

console.log('Product has the following properties');
_.each(product, function(val, key) {
    console.log(key + ":" + val);
});

No keys will be printed for a product that has a length of -1. Why? Well because internally underscore uses the length attribute, that every object in Javascript has to loop over all the attributes of the passed in object. Since we overwrote that value, it is now -1, and since we start looping at i = 0, the loop will never be executed.

Now for the question, how can I prevent the length property from being overridden? Best practices to prevent this from happening would also be appreciated.

  • 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-07T06:53:42+00:00Added an answer on June 7, 2026 at 6:53 am

    try this:

        var product = {
            name: "MacBook Pro 15 Inch",
            description: 'The new macbook pros....',
            length: 15,
            height: 15,
            weight: 4
        };
        console.log('Product has the following properties');
        _.each(_.keys(product), function(key){
            console.log(key + ":" + product[key]);
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found myself recently using the SemaphoreSlim class to limit the work in progress
I've recently found myself using the following macro with gcc 4.5 in C++11 mode:
I found myself using this: $var=(string)array_shift(array_values($item->xpath($s))); where $s is an xpath search string and
I just found myself absent-mindedly using from as an identifier. I realise that it
I'm working on a project and have found myself in a situation where I
I found myself in a desperate situation trying to understand the algorithm below. Does
I found myself in a situation where I know what type something is. The
I just started out using JSF 2.0 but I found myself mixing JSF tags
I found myself using a lot of individual bindings to my App class for
I found myself writing the following a lot: int location =2; vector<int> vec; vector<int>::iterator

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.