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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:15:16+00:00 2026-06-05T14:15:16+00:00

I am having some Problems with JavaScript. I have got the following code: <html>

  • 0

I am having some Problems with JavaScript. I have got the following code:

<html>
<head>
<title>Test</title>
<script  type="text/javascript">
function Control(){
    var name;

    this.setName = function(newName){
        name = newName;
    };

    this.getName = function(){
        return name;
    };
}

function SpecializedControl(){
}
SpecializedControl.prototype = new Control();

function Form(){
    var formControls = [];

    this.addControl = function(control){
         formControls.push(control);

         alert(formControls[0].getName());
    };
}

var form = new Form();

var control1 = new SpecializedControl();
control1.setName("Control1");
form.addControl(control1);

var control2 = new SpecializedControl();
control2.setName("Control2");
form.addControl(control2);

</script>
</head>
<body>
</body>
</html>

The SpecializedControl inherits from the Control class.

The addControl function in the Form class just adds the control to an array.

The problem is that when I add more than one SpecializedControl, the values in the array are kind of overriden, that means that when I access the first item in the array, which should be “Control1” I get “Control2”. Control1 is not in the array any longer.

When I use the same function with Control objects as parameters everything works as expected.

Does someone know why this happens and what can be done to correct 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-05T14:15:18+00:00Added an answer on June 5, 2026 at 2:15 pm

    The values in the array aren’t being overridden; the problem is both controls share the same name variable. Because the Control function only executes once, there is only one name variable ever declared.

    You have two main options to fix this. (1) Make name an instance variable which is specific to each individual control (e.g. this._name). (2) Execute the Control function from inside the SpecializedControl constructor. (Actually, IMO, for a well-balanced and thorough inheritence model you should be doing a bit of both of these methods).

    Here are three working solutions. The first two use options (1) and (2) respectively. The third combines both methods and is the way I would do it (but requires joi).

    Option 1:

    function Control(){
    
        this.setName = function(newName){
            this._name = newName;
        };
    
        this.getName = function(){
            return this._name;
        };
    }
    

    Option 2:

    function SpecializedControl(){
        Control.apply(this, arguments);
    }
    

    Option 3:

    var Control = joi.Unit.sub(function() {
    
        function constructor() {
            this.base();
        }
    
        constructor.prototype = {
            '#name': null,
            setName: function(name) {
                this['#name'] = name;
            },
            getName: function() {
                return this['#name'];
            }
        };
    
        return constructor;
    
    }());
    
    var SpecializedControl = Control.sub(function() {
    
        function constructor() {
            this.base();
        }
    
        return constructor;
    
    }());
    
    var Form = joi.Unit.sub(function() {
    
        function constructor() {
            this.base();
            this['#formControls'] = [];
        }
    
        constructor.prototype = {
            '#formControls': null,
            addControl: function(control) {
                this['#formControls'].push(control);
                alert(this['#formControls'][0].getName());
            }
        };
    
        return constructor;
    
    }());
    
    var form = new Form();
    
    var control1 = new SpecializedControl();
    control1.setName("Control1");
    form.addControl(control1);
    
    var control2 = new SpecializedControl();
    control2.setName("Control2");
    form.addControl(control2);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, I seem to be having problems with booleans in
I am having problems with code written in ASP.NET with some javascript, doing a
I'm having some problems with the resize of the Google Maps JavaScript API v3.
I'm having some real problems with a group of HTML Radiobuttons when I try
I'm having some Google Maps/Javascript problems. I think I know what the trouble is
I'm having some problems with implementing javascript controls in my asp.net page. I would
I am having some problems with Internet Explorer. I have a Window with one
I'm practicing object oriented syntax in javascript and am having some problems. This is
Having some problems with getting some ajax script to work. Using this as the
I have been having some problems with this for a few days now... I

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.