写法简化
let obj = {};
let obj1 = new Object();
let obj2 = Object.create(null); // 传入原型
var obj = {
name: 'zhangsan',
sleep: function() {},
// 方法书写方式增强
eat() {
console.log(1);
}
}
obj.eat();
let firstName = 'ai';
let secondName = 'xiaoye';
let name = 'ai xiaoye';
let person = {
[firstName + secondName]: name
}
console.log(person);
// { aixiaoye: 'ai xiaoye' }
⭐️ 对象拓展
obj[xxx]
,此处 xxx
都会隐式转化为字符串
var obj = {};
obj[true] = 'f'; // 会转成字符串'true'
obj[3] = 'b';
obj[obj] = 'bb';
console.log(obj['true']);
const a = {a: 1};
const b = {b: 2};
const obj = {
[a]: 'valueA',
[b]: 'valueB'
}
console.log(obj);