javascript 尝试从JS [duplicate]中的函数传递对象数据时出现问题

9lowa7mx  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(106)

This question already has an answer here:

String Interpolation in an Object (1 answer)
Closed 16 hours ago.
I am doing some JS challenges and am a stuck on one of the questions, for some reason the function argument isn't available and its blank in VS code and getting some unexpected results.

function myFunction(a, b) {

    return {a:b}
    
}

console.log(myFunction('b','w'))

//Expected result {b:'w'}

//What I am getting in console {a: 'w'}
fumotvh3

fumotvh31#

看起来您正在尝试动态设置密钥。请尝试此操作。

function myFunction(a,b) {
    return {[a]:b};
}

console.log(myFunction('b','w'))

相关问题