Naming Objects in JS
How do I create dynamic names for obj开发者_开发技巧ects in JS? I am pulling elements from a form (ex: First Name). I would either like to assign a number as the object name or just use the first name.
I'm not sure about variable names, but you can do so with property names
var string = "propertyName";
var obj = {};
obj[string] = "some value";
document.write(obj.propertyName); // some value
As to your specific use case
It might be better to use an array to maintain a list of users...
var users = [];
var newUser = { foo: "foo" , bar: "bar" };
users.push(newUser);
var anotherUser = { foo: "foo" , bar: "bar" };
users.push(anotherUser);
This gives you an array of users.
精彩评论