Ninjadoodle Posted June 1, 2016 Share Posted June 1, 2016 Hi guys Just wondering if something like this is possible? var test = 1; vat testAgain = 2; console.log(test+"Again"); How would I make something like this work? Thank you heaps in advance! Link to comment Share on other sites More sharing options...
ecv Posted June 1, 2016 Share Posted June 1, 2016 Apparently JS can't use macros as C does, see: http://stackoverflow.com/questions/16426295/how-to-define-something-in-javascript so I thought what you asked about wouldn't be possible, but then, here they're using concatenated indices or keys and it works: http://stackoverflow.com/questions/12149233/concatenate-a-dynamic-variable-name-in-javascript Quote > var a = 1; > var b = {}; > b['fruit_' + a] = 'apple'; > b.fruit_1 "apple" > b['fruit_1'] "apple" Note however the first comment to this question: Quote 99% of the time this is not necessary. What are you trying to accomplish? My original answer was going to be along those lines before I researched it, and I still agree. There should be a cleaner way to do what you want. Link to comment Share on other sites More sharing options...
Ninjadoodle Posted June 1, 2016 Author Share Posted June 1, 2016 Hi @ecv Thank you for your reply! I have 4 'groups' called 'cat1', 'cat2', 'cat3' and 'cat4'. Each cat has a bunch of body parts, a hitmap, and a sign that has four frames, each with a cat's name. I want to check if 'cat1Hitmap' was clicked - 'cat1Sign' changes to the next frame. I can easily do it with if statements in this case, but I wanted to see whether there was a quicker way - in case I have more sprites, I need to do this with in a future level. I tried ... this.onTouch = function(currentSprite) { console.log([currentSprite + 'Sign']) } But all I get in the console is - [Log] ["[object Object]Sign"] Thanks again! Link to comment Share on other sites More sharing options...
ecv Posted June 1, 2016 Share Posted June 1, 2016 Sorry but this is beyond my understanding. I'm new to Phaser and done very little JS in my life. I don't know if these groups and other names you're talking about are a Phaser's feature or some concept. All I can say is, since Phaser and all other JS engines/libraries for that matter seem to be object oriented, there should be nothing stopping you from defining (in JS way) a cat class with all these elements (hitmap, sign...) and instancing cat1, cat2, etc... to then access their properties. As I said I have no idea how to do this myself currently as I'm to all effects a newbie in JS and JS game engines, but I guess it's a matter of researching. As for using the keys solution mentioned before, I'd think you'd do something like this (but I think it's ugly): var whichCat=1; var cats = {}; cats['cat'+whichCat+'Hitmap'] = 'blah'; //cats[cat1Hitmap]='blah'; cats['cat'+whichCat+'Sign'] = 'bleh'; //cats[cat1Sign]='bleh'; Talking about which, I think 'cats' in this case or 'b' in the previous one, is a JS object. Anyways I would refrain from using this. Use a class cat class instead, instance your cat objects and assign/read there. If you really need to compose your objects' names, make all your objects part of a 'cat' array. Then you can traverse this array (I believe JS allows unique keys besides index numbers) any way you like and access each object elements/properties. Note this is pseudo-code, JS syntax could differ: class cat = { var Hitmap; var Sign; }; var catsArray; for (int i=0; i<100; i++) { catsArray[i] = new cat(); //Instancing //Assigning catsArray[i].Hitmap = 'hello'+i; catsArray[i].Sign = whatever; } //Accessing var myCat=1; print (catsArray[myCat].Hitmap); //output: "hello1" I don't think I'll be able to help any further. I hope I haven't introduced much confusion. Link to comment Share on other sites More sharing options...
Milton Posted June 1, 2016 Share Posted June 1, 2016 @ecv Ninjadoodle wants to use the variable name, not the actual value. It can be done: http://stackoverflow.com/questions/789675/how-to-get-class-objects-name-as-a-string-in-javascript Link to comment Share on other sites More sharing options...
mattstyles Posted June 1, 2016 Share Posted June 1, 2016 Your post does not make it clear at all what you want to do, you'll need to clarify, preferably with an example input and the output you expect, for example: var test = 1 console.log( test + 'again' ) // 1again console.log( test, 'again' ) // 1 again Both of these are perfectly valid output, or do you want to output console.log( test ) // 'test' You can't grab the key of an individual variable, but I'm not sure why you'd ever want to. You can grab the keys of an object though console.log( Object.keys( mySprite ) ) Objects in JS are akin to associative arrays, they just hold keys and values, keys are strings (or symbols, but forget about that for now) and values can be of a few different types. Arrays in JS are pretty much the same but their keys are integers and they have a few extra methods on the prototype (this is why a `typeof [ 1, 2 ]` will return 'object'). ES2015 adds Maps which are key-value associative arrays where the values and the keys can be of any type, there are also weak maps, but I don't think these are what you are after. In your onTouch example: this.onTouch = function(currentSprite) { console.log( currentSprite, 'Sign' ) } Standard console logging will parse an object into text and display that, if you try to add 'Sign' to an object then you get the raw text rep of the object '[object Object]' as object and string can not be added together, JS will attempt type coercion which ends up turning your object into a string ([object Object]). What do you want to add 'Sign' to?, for example If it is the keys then simply iterate over the keys using Object.keys, you'll get strings which you can add your custom 'Sign' string to, if you want all values of the object then again use Object.keys to grab all the keys then use those keys to add 'Sign' to the values of the object. You might want to filter by those keys on your actual object, or maybe you want your object plus all of its prototype methods. ecv and WombatTurkey 2 Link to comment Share on other sites More sharing options...
Ninjadoodle Posted June 1, 2016 Author Share Posted June 1, 2016 Hi guys Thank you all for your replies, I really appreciate your help! Sorry, I realize my first example wasn't very clear. I'm looking to use the name of the variable (not the value), and add some text to it (if this is possible). PS. I'm looking through you suggestions and experimenting Link to comment Share on other sites More sharing options...
bruno_ Posted June 1, 2016 Share Posted June 1, 2016 You have a global variable, so you have to use window["testAgain"]. You should organize your code in something like: var myObject = { first: "hello", second: "sec" } And then you can write: console.log(myObject["first"] + myObject["second"]) it should output the same as: console.log(myObject.first + myObject.second) Link to comment Share on other sites More sharing options...
Recommended Posts