console.log(`Mt. ${this.name} is ${this.elevation} feet tall`)
}
var funHike = {name,elevation,print}
funHike.print() // Mt. Tallac is 9738 feet tall
注意,这里我们是使用关键字
this
访问对象属性的。
当定义了对象方法后,就不需要再使用关键字
function
了(参见示例
2-5
)。
示例
2-5
:新旧对象语法比较
//
旧方案
var skier = {
name: name,
sound: sound,
powderYell: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.