开发者

JavaScript: Custom property with vars

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;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜