最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use variable for property name in JavaScript literal? - Stack Overflow

programmeradmin0浏览0评论

I know we can create new properties in Javascript objects during runtime but could that property be assigned a value too? For example.

var value = "New value";

var table = new Object();

var newValue = table[value];

Now, I know that value table has a new property called "value". but does that "value key contains the information as " New Value". So, does that mean now table object is like following:

table = {
value:"New Value";
}

I know we can create new properties in Javascript objects during runtime but could that property be assigned a value too? For example.

var value = "New value";

var table = new Object();

var newValue = table[value];

Now, I know that value table has a new property called "value". but does that "value key contains the information as " New Value". So, does that mean now table object is like following:

table = {
value:"New Value";
}
Share Improve this question edited Apr 5, 2019 at 6:57 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Oct 3, 2011 at 17:45 VikVik 4862 gold badges10 silver badges19 bronze badges 2
  • 2 btw, object literal notation {} is way nicer than new Object() in almost any possible way. – hugomg Commented Oct 3, 2011 at 17:49
  • Also, a semicolon (;) inside an object literal { ... } is invalid. Use mas (,) to separate properties INSIDE object literals. – Rob W Commented Oct 3, 2011 at 17:51
Add a ment  | 

3 Answers 3

Reset to default 5

You're confusing accessing with assigning.

// Assigns a variable named 'value' with a value of 'New Value'.
var value = "New value";
// Creates a variable named 'table' as a blank Object.
var table = new Object(); // Alternatively - table = {};
// Attempts to access "New Value" from object "table" which returns undefined.
var newValue = table[value];

If you want to assign properties to an object you do so like this:

// Assumes table is still an object.
table['key'] = 'value';

// Note that I almost _always_ opt for the variable['key'] notation over
// the variable.key notation because it allows you to use keys
// that would otherwise not be valid as identifiers.
table['Some Key'] = 'Some Value'; // This works.
table.Some Key = 'Some Value'; // This does not.

Later, when you want to retrieve that value and store it in a new variable, that's when you do this:

var newVariable = table['key'];

Hopefully that clarifies some. Please let me know if I can expand on any part of it.

no. your statement

var newValue = table[value];

is not setting anything, and since at the time when you created table you didn't assign any property, newValue will be undefined.

If you have a value variable that is assigned a value, and you want to assign that value to table under the key value, you want to do

table['value'] = value;

or alternatively

table.value = value

Erm, no, I don't think you've got it quite right.

All that does is assign undefined to newValue, because you're trying to access table's "New Value" property, which doesn't exist.

What I think you're trying to do is this:

var value = "New value";
var table = {};
table.value = value;
发布评论

评论列表(0)

  1. 暂无评论