mardi 24 mars 2015

Javascript, Best method for creating object with vars


I am creating an object in a function - am passed 3 vars, 1 is a name, 2 is a name, and 3 is an array.


So right now I create this object like so



createObj(var, var2, var3){
var temp1 = {};
var temp2 = {};

temp2[var2] = var3;
temp1[var] = temp2;
}


Which creates and object like



{var1 : {var2 : var3 }};


So this works just fine (I want the object looking like that), however I'm wondering if there is a way to do this without using so many variables to do it? I am using underscore if this makes it easier. It would be cool if there was a much more elegant (and hopefully) simple way to achieve this. I mean, this method works fine for me, however I am trying to improve my javascript skills, and use less code when possible :). Thanks!





Aucun commentaire:

Enregistrer un commentaire