【发布时间】:2020-01-11 11:38:01
【问题描述】:
尝试运行 cow1.voice(); 时,控制台中不断出现错误。
Uncaught ReferenceError: type is not defined
class Cow {
constructor(name, type, color) {
this.name = name;
this.type = type;
this.color = color;
};
voice() {
console.log(`Moooo ${name} Moooo ${type} Moooooo ${color}`);
};
};
const cow1 = new Cow('ben', 'chicken', 'red');
【问题讨论】:
-
它在
type而不是name上失败的原因是因为name指的是window.name。 (这并不能回答问题,但可能仍然有用。)
标签: javascript instance-variables es6-class