dynamic javascript variables

C++, C#, Java, PHP, ect...
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

dynamic javascript variables

Post 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()!!!!!!!!!!!!!
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: dynamic javascript variables

Post by Chris »

You could have pretty much left dynamic out of the topic name :P. 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);
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: dynamic javascript variables

Post 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 :P
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: dynamic javascript variables

Post 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! :D
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: dynamic javascript variables

Post 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);
}
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”