Can an object property be named directly within the object declaration instead of afterwards?
For example, this syntax works:
var name = "foo";
var obj = {};
obj[name] = "bar"; // obj.foo === "bar"
But is there a way to define it inside the object itself, like this:
var name = "foo";
var obj = {
name: "bar" // obj.name === "bar"
};
I am aware that strings can be used for property names, so I attempted a workaround, but it failed:
var obj = {
"" + name: "bar"
};
Is there a method to accomplish this task?