0

I am not very sure how to name the question. What i am trying to achieve is this..

I have a set of Global Variable, they will need to be replicated over and over, but assigned with different set's name example. For example

var start
var end
var time

And i have many set/model that i have to create and change, so i am wondering if it is possible to create 1 set and i just have a var modelnumber which then i can just copy and paste them and change the modelnumber so i wont have to change thousands of variable names?

Example

var modelnumber = "1";

var modelstart = modelnumber + "modelstart";
var modelend = modelnumber + "modelend";
var modeltime = modelnumber + "modeltime";

Edit: To provide more info

So i have model1.js , model2.js model3.js and so on....and all the variable names function names are the same, and to save me time, i want to write 1 set of code that i can just change the var modelname at the top of each field so i wont have to change the thousands of variable names and function names..

2
  • 4
    Don't use separate variables. Use one object with separate properties. If you have "thousands" of parallel start/end/time variables then you probably should be using an array of objects. Commented Feb 24, 2017 at 6:00
  • 1
    I agree with what @nnnnnn said. can you provide a little extra details about what you're trying to implement? Perhaps a sample of the set/model that you're describing? Commented Feb 24, 2017 at 6:05

3 Answers 3

1

You can always write a function:

function createVariables(modelNumber) {
  window[modelNumber + 'modelstart'] = 1;
  window[modelNumber + 'modelend'] = 2;
  window[modelNumber = 'modeltime'] = 3;
}
createVariables(1);

Or change it to however you want. :)

UPDATE: (use global in place of window for NodeJS).

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks for the info, i am trying to understand how it works, so what is that 1,2,3 ? The variable name becomes 1modelstart 2modelend and 3modeltime?
The 1, 2, 3 at the end is just values. You can set it to anything (or undefined).
The variable name is 1modelstart, 1modelend 1modeltime
Calling createVariables(2) will create 2modelstart, 2modelend, 2modeltime and so on. The takeaway is, just create a function to create any variables you want by using the window[...] or global[...] syntax
Main thing is organization and name conflict. The general practice is to not polluting the global namespace. As you may override global variables that is used by other libraries. My answer is simply doing what you asked, but as now you get it working, you should think about how to clean it up. :)
|
1

I think you're looking for a normal object literal. You can specify the property keys of the object with strings, which will give you the dynamic effect you're looking for.

Here's an example, using a for loop to populate the object.

var models = {};
var number_of_keys = 1000;

for(var i = 1; i < number_of_keys; i++) {
  var keyName = 'model' + i;
  var model = {
     'start': i + 'modelstart',
     'end': i + 'modelend',
     'time': i + 'modeltime'
   }
   models[keyName] = model;  
}

console.log(models);

Update: As an example of how you could access your populated models, consider the following:

// You can effectively replace the `1` in this example with any number:
var model1 = models['model1'];

// model1 would be:
// {
//   'start': '1modelstart', 
//   'end' : '1modelend', 
//   'time': '1modeltime'
// }

var start1 = model1.start;
var end1 = model1.end;
var time1 = model1.time;

// Pseudo-code
var modelN = models['modelN'];
var startN = modelN.start;
var endN = modelN.end;
var timeN = modelN.time;

HTH

1 Comment

This (or possible a variation using an array of the inner objects) is definitely the right approach. But it would be nice to add an example of how to later access the values.
0

You could (should?) use an object or an array of objects.

For example:

// The "Model"
var Model = function(start,end,time) {
    this.start = start;
    this.end = end;
    this.time = time;
}

// One option.
// Assign "Model" to the models
var models = {
    'm1': new Model(x,y,z),
    'm2': new Model(a,b,c)
}

// Access values
if (models.m1) {
    alert("m1 end:["+ models.m1.end +"]");
}

// Add a "new" model
models['ace'] = new Model(r,s,t);
// or even
models.club = new Model(e,f,g);

You could also extend it like so:

Model.prototype.debug = function(id) {
    if (id) {
        console.log("model id:["+ id +"]");
    }
    console.log("start:["+ this.start +"]");
    console.log("end:["+ this.end +"]");
    console.log("time:["+ this.time +"]");
}

Which you would call like so:

models.m1.debug();

Or even:

for(x in models) {
   models[x].debug(x);
}

Here is a code snippet example.

var Model = function(start,end,time) {
    this.start = start;
    this.end = end;
    this.time = time;
}

Model.prototype.debug = function(id) {
    if (id) {
        console.log("model id:["+ id +"]");
    }
    console.log("start:["+ this.start +"]");
    console.log("end:["+ this.end +"]");
    console.log("time:["+ this.time +"]");
}

var models = {
    'm1' : new Model('x','y','z'),
    'm2' : new Model('a','b','c')
};

models.ace = new Model('r','s','t');

for(x in models) {
    models[x].debug(x);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.