class Comment {
text='igor';
age=25;
}
user1 = new Comment(); }
По факту при возові обїекта Comment викликається функція конструктора. Тому туди можна передавати аргументи у влстивості
class Comment {
constructor (text,vot) {
this.text = text;
this.votesQty = vot;
this.any = 'any_text';
}
upvote() {
this.votesQty += 1;
}
}
user1 = new Comment('igor',25);
const firstcomment = new Comment('First comment')
firstcomment.upvote()
firstcomment instanceof Comment firstcomment instanceof Object)
Comment.prototype.constructor
Ця властивість буде доступна как свойство только классаи НЕ наледуются всеми єкземплярами класса. т.е. он будет доступен как свойство только самого класса
class Comment {
constructor (text) {
this.text = text;
this.votesQty = 0;
}
static pi = 3.1415;
}
fdf = Comment.pi;
єтот метод будет доступен как свойство класса Comment и НЕ наледуются всеми єкземплярами класса. т.е. он будет доступен как метод только самого класса
class Comment {
constructor (text) {
this.text = text;
this.votesQty = 0;
}
static mergeComment (first, second) {
return `${first} ${second}`;
}
}
Comment.mergeComment("asfsdf","sfdgdfg")
Приватні властивості описувати зпочатку перед конструктором через знак # і звертатись до них також у середині класу.
class Comment {
#maxSpeed = 300;
constructor (text,vot) {
this.text = text;
this.votesQty = vot;
this.any = 'any_text';
}
ShowMaxSpeed() {
console.log(#maxSpeed);
}
}
user1 = new Comment('igor',25);
user1.ShowMaxSpeed();
В більшості вони потрібні тільки для приватних властивостей, але технічно можна робити і не для приватних властивостей
class Comment {
#age= 30;
constructor (text,vot) {
this.text = text;
this.votesQty = vot;
this.any = 'any_text';
}
set age(newValue){
this.#age = newValue;
}
get age(newValue){
return this.#age;
}
}
Comment.age = 10; Console.log(Comment.age);
//--Батьківкий клас
class Animal {
constructor(name, age, pows){
this.name = name;
this.age = age;
this.paws = pows;
}
sleep(){
console.log('zzzz')
}
}
//--наслідумо у класс Dog властивості та методи (по факту методи не копіюються але система по ланцюжку шука у батьківських поки не знайде) з класу Animal
class Dog extends Animal {
constructor (name, age, pows){
super(name, age, pows);
this.typevoice = '';
}
satHello(){
console.log('PowPow');
}
}