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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:57:25+00:00 2026-06-11T00:57:25+00:00

Had a strange bug appear today. In our app, on one file, all the

  • 0

Had a strange bug appear today. In our app, on one file, all the objects got a function attached, as if they were prototyped. We’re not prototyping our arrays but I’ve worked on other stuff where the arrays and objects had prototypes and when you did a for(this in that) loop the prototypes would be included as items and we had to filter them. So, two part question:

  1. If you prototype your arrays will the index increase by + # of prototypes? So if array(a,b,c) has array.prototype.function(), would it’s index count be 3 or 4?

  2. Should this be happening?

We’re thinking one of the jquery plugins we have is being all evil.

  • 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-11T00:57:27+00:00Added an answer on June 11, 2026 at 12:57 am

    By “index count” I assume you mean length property. In which case, the answer to your first question is “no.” The length property is tied to the highest numerical index of an array. So, for instance, if you have the following code, arr.length will be 4:

    var arr = [];
    arr[3] = true;
    arr.length; //is 4
    

    However, you referred to for-in loops, which have strange behavior in this situation. If we perform a for-in on arr (from above), we get the following:

    var output = "";
    for (i in arr) { //Don't do this!
        output += i;
    } 
    output; //is "3" (not "0123" as you might expect)
    

    As I mentioned, prototyped properties have no impact on the length:

    Array.prototype.foo = true;
    var arr = [];
    arr[3] = true;
    arr.length; //is 4
    

    However, they do impact for-in loops (under normal circumstances):

    var output = "";
    for (i in arr) { //arr from the previous code block.
        output += i;
    } 
    output; //is "3foo" (or maybe "foo3", there's no guarantee form the spec either way)
    

    There are a few ways to get around this. First of all, you should really never be using for-in on array objects because of this kind of nonsense. But if you MUST do it for some reason, you should always protect your for-in loop using a hasOwnProperty() filter, like so:

    var output = "";
    for (i in arr) { //arr from the previous code block
        if (arr.hasOwnProperty(i)) {
            output += i;
        }
    } 
    output; //is "3"
    

    Again, there’s no guarantee of ordering on for-in. So if arr was [1,1,1], you might get output as "201" (though it’s unlikely).

    Another option is to try and save yourself from others’ bad coding by using a feature of ES5 (if present) to protect your prototyped properties on the object level by setting them to be non-enumerable. You would do that like this:

    var foo = (function() { /* do something */ });
    if(typeof Object.defineProperty === "function") { //If ES5
        Object.defineProperty(Array.prototype, "foo", {
            value: foo,
            enumerable: false //make it not enumerable
        });
    } else { //For older browsers
        Array.prototype.foo = foo;
    }
    
    var arr = [], output = "";
    arr[3] = true;
    
    for(i in arr) { //Seriously, don't do this.
        output += i;
    }
    output; //is "3"
    

    I cannot stress enough that doing a for-in over an Array is a bad idea. In fact, I would tend to avoid for-in under pretty much all circumstances because you never know if someone out there wants to be a jerk and will toss this into the middle of their code:

    Object.prototype.ponies = "Ponies!";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was working on a program today and hit this strange bug. I had
I had a strange problem in one of our production databases. Long story short,
I had a strange error in a VB6 app this morning and it all
I had a strange bug when porting a feature to the Python 3.1 fork
I had a strange bug i don't understand, and changing LINQ's IEnumerable to list
I'm having fun with dates in flex, and had a strange bug showing up.
I've got a strange bug I'm not sure how to track down. I'm running
We had a strange bug report from a user who has iOS 4.2.1 on
I had a strange problem. I have a file of the format: START 1
We had discovered strange behaviour of popups in IE8. In IE7 and IE6 our

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.