对象字面量

205 阅读1分钟

// 对象字面量

 const bar = '345' 

 const obj = { 

 foo: 123,

 // bar: bar

 // 属性名与变量名相同,可以省略 : bar bar, 

 // method1: function () {

 // console.log('method111') 

 // } 

 // 方法可以省略 :

 function method1 () {

 console.log('method111') 

 // 这种方法就是普通的函数,同样影响 this 指向。 console.log(this) }, 

 // Math.random(): 123

 // 不允许

 // 通过 [] 让表达式的结果作为属性名 [bar]: 123 } 

// obj[Math.random()] = 123

 console.log(obj)

 obj.method1()