仅含getter的JavaScript ES6类
我想问一下,在ES6中,如何仅使用getter而不使用setters(readOnly)属性?为什么Webstorm告诉我这是一个错误 这是我的密码:仅含getter的JavaScript ES6类,javascript,ecmascript-6,getter-setter,getter,es6-class,Javascript,Ecmascript 6,Getter Setter,Getter,Es6 Class,我想问一下,在ES6中,如何仅使用getter而不使用setters(readOnly)属性?为什么Webstorm告诉我这是一个错误 这是我的密码: class BasePunchStarter { constructor(id,name,manufacturer,description,genres,targetPrice) { if (new.target==BasePunchStarter) { throw new TypeError("
class BasePunchStarter {
constructor(id,name,manufacturer,description,genres,targetPrice) {
if (new.target==BasePunchStarter) {
throw new TypeError("BasePunchStarter class cannot be instantiated directly!");
}
if (typeof id =="number") {
// noinspection JSUnresolvedVariable
this.id = id;
} else throw new TypeError("ID must be a number!");
if (typeof name=="string") {
// noinspection JSUnresolvedVariable
this.name = name;
} else throw new TypeError("Name must be a string!");
if(typeof manufacturer=="string") {
// noinspection JSUnresolvedVariable
this.manufacturer = manufacturer;
} else throw new TypeError("Manufacturer must be a string!");
if (typeof description=="string") {
// noinspection JSUnresolvedVariable
this.description = description;
} else throw new TypeError("Description must be a string!");
if(typeof genres=="Object") {
// noinspection JSUnresolvedVariable
this.genres=genres;
} else new TypeError("Genres must be an Array of strings!");
if (typeof targetPrice=="number") {
// noinspection JSUnresolvedVariable
this.targetPrice = targetPrice;
} else new TypeError("Target price must be a number!");
this.accumulatedMoney=0;
}
get accumulatedMoney() {
return this._accumulatedMoney;
}
set accumulatedMoney(money) {
this._accumulatedMoney=money;
}
get id() {
return this._id;
}
get name() {
return this._name;
}
get manufacturer() {
return this._manufacturer;
}
get description() {
return this._description;
}
get genres() {
return this._genres;
}
get targetPrice() {
return this._targetPrice;
}
}
我已经放置了
//noinspection jsunsolvevariable
以抑制警告。但是应该有比这更好的解决方案。似乎您正在将构造函数上的值分配给getter,而不是以下划线为前缀的支持字段
constructor(id,name,manufacturer,description,genres,targetPrice){
if(new.target==BasePunchStarter){
throw new TypeError("BasePunchStarter class cannot be instantiated directly!");
}
if(typeof id =="number") {
// use the backing field instead.
this._id = id;
[..]
如果您还没有这样做,您应该在使用支持字段之前声明它们。您的代码不是惯用的JS。该语言是松散类型的,其原理基于duck类型。您在构造函数中所做的事情很糟糕,应该避免。JavaScript不是Java。如果需要强静态键入,请使用或 getter和setter在ES6类中很容易使用,就像对象文本中的getter和setter一样 如果您想要只读属性,可以使用
\ucode>编码约定,只需避免设置器。如果我们从文档中选取一个简单的示例,我们会得到以下结果:
班级人员{
构造函数(名字,姓氏){
这个。_firstname=firstname;
这是。_lastname=lastname;
}
获取名字(){
返回此。\u firstname;
}
获取lastname(){
返回此。\u lastname;
}
}
let person=新人(“约翰”、“多伊”);
console.log(person.firstname,person.lastname);//无名氏
//这被忽略了
person.firstname='Foo';
person.lastname='Bar';
console.log(person.firstname,person.lastname);//John Doe
如果您使用的是ES6类,那么至少您也可以使用=
。此外,作为可靠编程的一点,抛出构造函数是一个非常糟糕的计划。这不是建造师的责任。如果您有类型要求,请确保您正在传递已经验证过的数据(无论如何,您都应该这样做),或者使用类似于TypeScript的东西,这样您就可以获得类型安全性。脱离主题,但是@Mike'Pomax'Kamermans,为什么您不应该抛出构造函数呢?这似乎是发出错误信号的最安全、最自然的方式(可能是也可能不是调用者的错误)。可能是不相关的错误:在if(typeof genres==“Object”){
,它应该是“Object”
,小写。@A.L.Flanagan诸如此类的事情比我在评论中能解释得更好。谢谢@Mike'Pomax'Kamermans,这实际上很有意义。我对你的答案投了赞成票,但在Javascript中,没有理由在分配属性之前声明属性。第三个示例不起作用,因为类的所有实例都引用了相同的变量生活如此让p1=新人('a','b');让p2=新人('c','d');console.log(p1.firstname,p1.lastname);
输出cd
而不是ab
。