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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:22:23+00:00 2026-06-02T01:22:23+00:00

I’m attempting to use dynamic variable names in JavaScript applied to a constructor. It’s

  • 0

I’m attempting to use dynamic variable names in JavaScript applied to a constructor. It’s been much more difficult than expected. I’ve read about this issue in numerous forums and webpages but I can’t find what I’m doing wrong.

I’m using a HTML5+JavaScript library called EaselJS but my problem it’s not related to it but to JavaScript syntax! Here’s my problem. I got this code:

stage1 = new Stage(document.getElementById("cnvs1"));
stage2 = new Stage(document.getElementById("cnvs2"));
stage3 = new Stage(document.getElementById("cnvs3"));

Here, the variables stage have assigned an Stage object which is initialized with canvas id cnvs This line (in the context of the rest of my code) works!

I’d like to simplify this to a single line with a for like this:

for(i = 0; i < 5; i++){
  //Of course this is wrong!
  stage[i] = new Stage(document.getElementById("cnvs[i]"));
}

This link here resumes how this can be done: With eval (a lot of people say it’s not recommendable) and with window (recommended) Then if I do this (without the for to simplify even more):

var varname = "stage";
var i = 1;
window[varname + i] = new Stage(document.getElementById("cnvs_terminal1"));

The code still works. But I can’t figure out how to accomplish a similar solution with the canvas id in quotes. These lines of code fail:

var i = 1;
var varname = "stage";
var varname2 = 'cnvs1';
var varname3 = "\"cnvs1\"";
var varname4 = "\"cnvs1" + i + "\"";

window[varname +i] = new Stage(document.getElementById(window[varname2]));
window[varname +i] = new Stage(document.getElementById(window[varname3]));
window[varname +i] = new Stage(document.getElementById(window[varname4]));

In these attempts even trying to pass the exact value I need (without the i) does not solve the problem. I’m backslashing quotes because I think they are necessary for the getElementbyId.

I’m certain there is a way to do this, maybe I don’t know the syntax, maybe I don’t know how to call variables with reference or by value properly, I don’t know…

Edit

I am trying @cHao’s suggestion, but I can’t do that either! Keeping it simple, I set the for for i to be one only:

for (var i = 1; i <= 1; ++i) {
  //I use only one of the following lines at a time of course, but I'll write here my failing tries all together:
  stage[i] = new Stage(document.getElementById('cnvs'+i));
  stage[i-1] = new Stage(document.getElementById('cnvs'+i));

  //Hardcoding the first variable works!
  stage1 = new Stage(document.getElementById('cnvs'+i));
}

Half of the problem is solved, but what to do with the first variable?

Edit 2

More detail has been requested. Here’s a copy/paste example. Just update the EaselJS and an image paths for this to work:

<!DOCTYPE HTML>
<html>
  <head>
    <script src="easeljs-0.4.0-26/lib/easel.js"></script>       
        
    <script type="text/javascript"> 
      function init() {
        //MY PROBLEM LIES HERE!!
        for (var i = 1; i <= 1; ++i) {
          //This code WORKS
          stage1 = new Stage(document.getElementById('cnvs'+i));
          //This code DOES NOT WORK
          //stage[i] = new Stage(document.getElementById('cnvs'+i));
          //stage[i-1] = new Stage(document.getElementById('cnvs'+i));
        }
        var images = "images/itzapic.jpg";
        bitmap = new Bitmap(images);
        stage1.addChild(bitmap);
        stage1.update();
        Ticker.addListener(window);
      }
            
      function tick() {
        stage1.update();
      }
    </script>        
  </head>
    
  <body onload="init()"> 
    <canvas id="cnvs1" width="140" height="82">
  </body>
</html>
  • 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-02T01:22:25+00:00Added an answer on June 2, 2026 at 1:22 am

    Not sure i get the point of eval’ing or pre-figuring variable names and all that. Seems like you could just do like

    for (var i = 1; i <= 5; ++i) {
        stage[i] = new Stage(document.getElementById('cnvs' + i));
    }
    

    window["varname"] won’t help unless varname is global, and…eh. You’ll want to avoid globals if you can. Either way, you don’t need to escape quotes — you don’t need quotes in the name itself at all unless your IDs have quotes in them (which, AFAIK is invalid anyway) or you’re eval’ing (which is evil in itself).

    If you’re looking to put the stages into an array (which seems like a better goal), you’ll want your first stage to be stage[0]. In which case, set stage[i-1] instead of stage[i].

    And if you’re trying to set stage1, stage2, etc…stop. Don’t do that. You have a bunch of items, that you’re treating alike…that’s the kind of thing arrays were meant for. For one thing, notice how much easier it is to work with array elements than similarly-named variables? I wasn’t even seeing the issue, because with arrays it’s already a non-issue.

    As for your code…watch this.

    stage = [];
    
    function init() {
        for (var i = 1; i <= 1; ++i) {
            stage[i-1] = new Stage(document.getElementById('cnvs'+i));
        }
        var images = "images/itzapic.jpg";
        bitmap = new Bitmap(images);
        stage[0].addChild(bitmap);
        stage[0].update();
        Ticker.addListener(window);
    }
    
    function tick() {
        for (var i = 0; i < stage.length; ++i) {
            stage[i].update();
        }
    }
    

    Once we switch from the stage1 stage2 brokenness to using an array, now we can have as many stages as we want.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.