开发者

JavaScript: Custom property with vars

开发者 https://www.devze.com 2023-03-02 12:35 出处:网络
I have a JavaScript function like that: function someCoolActionHere(input) { return { input: \'someValue\' };

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

精彩评论

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

关注公众号