javascript ES6 class with getters only
Здравейте, искам да попитам защо ми дава грешка, че полетата са "Read only" , когато пиша само getter-и без setter-и в клас на ES6?
Това ми е кода:
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;
}
}
module.exports = BasePunchStarter;
Използвам "//noinspection JSUnresolvedVariable" за да не ми пищи за грешката и няма проблем, но едва ли е най-добрият вариянт за справяне с проблема.
Супер сиии!!! Мерси много!!!
