开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜