我对下面的代码有疑问,它会抛出一个类似语法错误的错误,但我遵循的指南说语法是正确的。不知道该怎么办,不明白,请帮忙。
以下是代码链接:
const car = {
maker: 'Ford',
model: 'Fiesta',
drive() {
console.log(`Driving a ${this.maker} ${this.model} car!`)
}
}
car.drive()
// the same above code can be written as:
const bus = {
maker: 'Ford',
model: 'Bussie',
drive: function() {
console.log(`Driving a ${this.maker} ${this.model} bus!`)
}
}
bus.drive()
// the same code above can be written in this way:
const truck = {
maker: 'Tata',
model: 'Truckie',
truck.drive = function() {
console.log(`Driving a ${this.maker} ${this.model} truck!`)
}
}
truck.drive()
// Now, let us see how the arror function works:
const bike = {
maker: 'Honda',
model: 'Unicorn',
drive: () => {
console.log(`Driving a ${this.maker} ${this.model} bike!`)
}
}
bike.drive()
错误:
SyntaxError: Unexpected token '.'
at line: truck.drive = function() {