const key = 'name'
const tempObj = {key: 'shiyuq'}
console.log(tempObj)
// {key: 'shiyuq'}
// but I want a object like this {name: 'shiyuq'}
// Finally I find a new way to modify object's key value,code follows like this:
const tempObj = {[key]: 'shiyuq'}
console.log(tempObj)
// {name: shiyuq'}
what does [ ]
mean and how does it work here?
thanks!
这是 ES6 里的计算属性(Computed property names)。
等效于:
const key = 'name';
const tempObj = {};
tempObj[key] = 'shiyuq';
等效于:
const tempObj = {};
tempObj['name'] = 'shiyuq';
等效于:
const tempObj = {};
tempObj.name = 'shiyuq';
https://developer.mozilla.org...