【发布时间】:2019-08-30 04:28:29
【问题描述】:
在键/值对中声明的方法和 JavaScript 对象中的声明性方法有什么区别,this 关键字在这两种方法中都起作用
let dog = {
sound: "roof",
talk: function(){ //<----------Key/Value
console.log(this.sound)
}
}
let dog = {
sound: "roof",
function talk(){ //<----------Declarative method
console.log(this.sound)
}
}
【问题讨论】:
-
区别是
ECMAScript 2015 -
区别在于第二个不是有效代码。
-
Uncaught SyntaxError: Unexpected identifier -
true - 第二个不需要
function关键字在talk()之前
标签: javascript object methods