I have a JavaScript function like that:
function someCoolActionHere(input) {
return { input: 'someValue' };
}
This function returns an JS object. The function parameter input
is a string (e.g. name
) and the value of the paramter variable should be used as property name in the object, not input
itself. Example:
someCoolAc开发者_StackOverflow社区tionHere('hello');
// => { 'hello': 'someValue' }
How could that be done?
Can't do it with a literal. You'll have to use []
to set the property.
var obj = {};
obj[input] = "someValue";
return obj;
What about this?
function someCoolActionHere(input) {
obj = {};
obj[input] = 'someValue';
return obj;
}
精彩评论