Page 1 of 1
dynamic javascript variables
Posted: Thu Mar 22, 2012 4:10 pm
by hallsofvallhalla
Okay so I have created a class in javascript
lets say creatures
i need to create new instances of this class dynamical. Like spawning a new creature
Code: Select all
var namerand = Math.floor(Math.random()*9999);
var creaturename = "creature" + namerand
creaturename = new axilict();
creaturename.positionx = 500;
creaturename.positiony = 500;
creaturelist.push(creaturename);
this however doesn't seem to work. How can I create the dynamic names and pt them into the array.
DO NOT SAY EVAL()!!!!!!!!!!!!!
Re: dynamic javascript variables
Posted: Thu Mar 22, 2012 6:31 pm
by Chris
You could have pretty much left dynamic out of the topic name

. Everything in JavaScript is dynamic.
Code: Select all
Creature = function(name, type, position)
{
this.name = name == undefined ? "" : name;
if( position instanceof Vector2 ) this.position = position;
if( type instanceof CreatueType ) this.type = type;
}
Creature.prototype = {
constructor : Creature, // Creature function
name : null,
type : null,
position : null,
stats : null
}
// my adivce would be to make a List object
CreatureList = [];
var myCreature = new Creature( "Random name", CreatureTypeList.axilict);
myCreature.position.set( 60, 9 );
CreatureList.push(myCreature);
Re: dynamic javascript variables
Posted: Thu Mar 22, 2012 9:12 pm
by hallsofvallhalla
thanks for the code!
Well technically
var myname is not a dynamic variable name because i am setting it to a static name while making the variable name depend on a random number makes it dynamic. At least in my definition

Re: dynamic javascript variables
Posted: Thu Mar 22, 2012 10:09 pm
by Jackolantern
It seems what you are asking is for something similar to "variable variables" in PHP. I don't believe JS has them, like most languages do not. However, the functionality is usually not required because the same functionality can be done with arrays since you can piece together the array index key at runtime out of string constants, string variables, the return values from funtions, or whatever else you can think of. Then you just have your object as part of an array instead of floating around free, but that is not of much concern in Javascript since arrays are first-class objects that can do anything any other object can!

Re: dynamic javascript variables
Posted: Fri Mar 23, 2012 8:25 am
by Chris
Do you want to clone the object?
Try something like this:
Code: Select all
Object.prototype.clone = function()
{
var Object = function(){};
Object.prototype = this;
return new Object();
}
var myCreature = new Creature("lol", CreatureTypeList.axilict);
var mySecondCreature = myCreature.clone();
mySecondCreature.name = "haha";
console.log(myCreature.name) // lol
console.log(mySecondCreature.name) // haha
To make a list of random creatures run a loop to make new instances or clones..
Code: Select all
var myCreatureList = [];
for( var i = 0; i <= 50; i++ )
{
var randomName = Math.floor( new Date().getTime() * Math.random() ).toString(16);
var creature = new Creature( randomName , CreatureTypeList.axilict );
myCreatureList.push(creature);
}
Code: Select all
var myCreatureList = [];
var myCreature = new Creature("", CreatureTypeList.axilict);
for( var i = 0; i <= 50; i++ )
{
var clonedCreature = myCreature.clone();
clonedCreature.name = Math.floor( new Date().getTime() * Math.random() ).toString(16); // random name
myCreatureList.push(clonedCreature);
}