Вы используете синтаксис класса внутри функции.
Вы можете создать функцию конструктора и добавить методы к a.prototype
function a(x, y) {
this.x = x;
this.y = y;
};
a.prototype.getX = function() {
return this.x;
}
a.prototype.getY = function() {
return this.y;
}
const newA = new a('1', '2');
console.log(newA.getX());
console.log(newA.getY());
Или создайте class
следующим образом:
class a {
constructor(x, y) {
this.x = x;
this.y = y;
}
getX() {
return this.x;
}
getY() {
return this.y;
}
};
const newA = new a('1', '2');
console.log(newA.getX());
console.log(newA.getY());
Другой вариант - создать getter
для X
и Y
:
class a {
constructor(x, y) {
this.x = x;
this.y = y;
}
get X() {
return this.x;
}
get Y() {
return this.y;
}
};
const newA = new a('1', '2');
console.log(newA.X);
console.log(newA.Y);