开发者

Naming Objects in JS

开发者 https://www.devze.com 2023-04-09 15:15 出处:网络
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

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

精彩评论

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

关注公众号