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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:26:29+00:00 2026-06-18T11:26:29+00:00

I am trying to create a list of objects that are listed in an

  • 0

I am trying to create a list of objects that are listed in an array. newConstant is a function that creates the objects and them pushes them to the array. However, when the while loop runs through the array and throws up alerts containing one of the properties of each array, it spits out the value for the last object for each object in the array. In this situation it alerts “3” each time, but it should alert “1”, then “3”, as those are the value of the property x for the two objects in the array “a”. The code is below. How can I fix this?

var i = 0;
var a = [];
var newConstant = function (x, y) {
    this.x = x;
    this.y = y;
    a.push(this);
};
var one = newConstant(1, 2);
var two = newConstant(3, 4);

while (i < a.length) {
    alert(a[i].x);
    i++;
}
  • 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-18T11:26:30+00:00Added an answer on June 18, 2026 at 11:26 am

    You’re written newConstructor as a constructor but you’re using it as a normal function, try adding the new keyword.

    var i = 0;
    var a = [];
    var newConstant = function (x, y) {
        this.x = x;
        this.y = y;
        a.push(this);
    };
    var one = new newConstant(1, 2); //notice the new keyword indicating a constructor
    var two = new newConstant(3, 4);
    
    while (i < a.length) {
        alert(a[i].x);
        i++;
    }
    

    Here it is in action: http://jsfiddle.net/V3zwW/

    Here is an article about the this keyword in javascript. Here is another reference on how to correctly use the Constructor pattern

    What happened before is that your second call set this.x to 3 However this referred to the window , this is because functions in javascript assign this to their caller unless they are constructors. In your case you alerted window.x (which you set to 3) two times resulting in 3 3

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

Sidebar

Related Questions

i am trying to create a REST service that accepts a List of objects
I am trying to create lambdas inside a loop that iterates over a list
I'm trying to create a list of permutations of a list, such that, for
I'm trying to create a list of tasks that I've read from some text
I am trying to create a list that adds a new row of data
I'm trying to create a list that some of the elements are defined and
I am trying to create a list box that displays a set of data
I'm trying to create a dynamic list of objects because I don't know what
I'm trying to create a generic list from a specific Type that is retrieved
I'm trying to create a list of objects in Python. The below code should

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.