[英]' ; ' expected when trying to create a prototype function call
我试图用构造函数和原型函数创建一个简单的Animal类,以返回Animal的名称和描述。
到目前为止,我已经为该类创建了构造函数:
class Animal {
Animal(name, description) {
this.name = name;
this.description = description;
}
}
但是当我尝试创建Animal原型并调用函数以返回Animal的名称和说明时...
Animal.prototype.message = function() {
return this.name " + has the following description: " + this.description;
}
... Visual Studio代码突出显示Animal.prototype.message()
的句点,并告诉我';' expected
';' expected
。
我已经呆了一个小时了,我觉得自己遗漏了一些明显的东西,但是不管我想知道我做错了什么。 在此先感谢您的帮助。
编辑:固定代码错字。
我在这里看到几个问题。
Animal
应该是constructor
) 我将收到的愚蠢错误是由于设置了“构造函数”(使用Animal
而不是constructor
),或者是因为您正在执行
Animal.prototype.message() = function { ... }
(应该是Animal.prototype.message() = function() { ... }
)
例:
class Animal { constructor(name, description) { this.name = name; this.description = description; } message() { return `${this.name} has the following description: ${this.description}`; } } const animal = new Animal('liger', 'a lion and a tiger'); const message = animal.message(); console.log(message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.