ES6 Classes

"Classes-ը բերեցին ավելի մաքուր և հասկանալի սինտաքս (syntactic sugar) նախատիպային ժառանգության վրա աշխատելու համար։"

class-ը փոխարինում է ES5-ի կոնստրուկտոր ֆունկցիաներին։ Այն թույլ է տալիս մեկ տեղում հայտարարել և՛ կոնստրուկտորը, և՛ մեթոդները։

class Animal {
  // Կոնստրուկտորը կանչվում է new օպերատորի ժամանակ
  constructor(name) {
    this.name = name;
  }

  // Մեթոդները ավտոմատ ավելացվում են Animal.prototype-ի մեջ
  sayHi() {
    console.log(Բարև, ես -ն եմ:);
  }
}

let dog = new Animal("Բոբիկ");
dog.sayHi(); // Բարև, ես Բոբիկ-ն եմ:

ES6-ում ժառանգությունը իրականացվում է extends բանալի բառի միջոցով։

class Rabbit extends Animal {
  constructor(name, speed) {
    // ՊԱՐՏԱԴԻՐ: Կանչում ենք ծնողի կոնստրուկտորը super()-ով
    super(name);
    this.speed = speed;
  }

  // Մեթոդի գերազանցում (Override)
  sayHi() {
    super.sayHi(); // Կանչում ենք ծնողի մեթոդը
    console.log(Ես վազում եմ կմ/ժ արագությամբ:);
  }
}

super() կանչը

Եթե ժառանգ կլասում գրում եք constructor, ապա super()-ը կանչելը պարտադիր է, քանի դեռ չեք օգտագործել this-ը։

Static Մեթոդներ

Պատկանում են հենց կլասին, այլ ոչ թե նրա օբյեկտներին։

static compare(a, b) {
  return a.date - b.date;
}

Private Հատկություններ (#)

# սիմվոլը հատկությունը դարձնում է իսկապես փակ։

class Coffee {
  #waterLimit = 200;
}

Getters & Setters

get name() { return this._name.toUpperCase(); }
set name(v) { this._name = v; }

Բազմամակարդակ և մի քանի ճյուղերով ժառանգությունը թույլ է տալիս ստեղծել բարդ համակարգեր, որտեղ ընդհանուր հատկությունները բարձրանում են վերև, իսկ մասնավորները՝ իջնում ներքև։

🧬 Ժառանգության աստիճանակարգը (Class Hierarchy)

classDiagram Animal <|-- Dog Animal <|-- Cat Dog <|-- Shepherd Dog <|-- Bulldog Cat <|-- Siamese Cat <|-- Persian class Animal { +String name +eat() +sleep() } class Dog { +bark() } class Cat { +meow() } class Shepherd { +guard() } class Siamese { +vocalize() }

💻 Կիրառման օրինակ (ES6)

// Մակարդակ 1: Հիմնական կլաս
class Animal {
  constructor(name) { this.name = name; }
  eat() { console.log(${this.name}-ը սնվում է...); }
}

// Մակարդակ 2: Ճյուղավորում
class Dog extends Animal {
  bark() { console.log(${this.name}-ը հաչում է: Վա՛ֆ-վա՛ֆ:); }
}

// Մակարդակ 3: Մասնագիտացված կլաս (Shepherd)
class Shepherd extends Dog {
  constructor(name, rank) {
    super(name); // Կանչում ենք Dog-ի կոնստրուկտորը
    this.rank = rank;
  }
  guard() { console.log(${this.name}-ը հսկում է տարածքը (Կարգավիճակ՝ ):); }
}

Prototype Chain View

myDog → Shepherd.prototype → Dog.prototype → Animal.prototype → Object.prototype։

1. ES6 Class Structure

graph TD C[class Rabbit] e1@--> M1[constructor] C e2@--> M2[method: sayHi] C e3@--> M3[method: hide] e1@{animate : true} e2@{animate : true} e3@{animate : true} style C fill:#f7df1e,color:#000

2. Internal Prototype Chain

graph BT Instance[bunny instance] e1@-- "[[Prototype]]" --> Proto[Rabbit.prototype] Proto e2@-- "[[Prototype]]" --> ParentProto[Animal.prototype] ParentProto e3@-- "[[Prototype]]" --> ObjProto[Object.prototype] e1@{animate : true} e2@{animate : true} e3@{animate : true} style Instance fill:#f7df1e,color:#000 style Proto fill:#61afef,color:#000 style ParentProto fill:#98c379,color:#000

Hoisting & Strict Mode

  • No Hoisting: Կլասը չի կարելի օգտագործել մինչև հայտարարելը։
  • Strict Mode: Կլասի ներսի կոդը միշտ աշխատում է "use strict"-ով։

Enumerability

Կլասի մեջ սահմանված մեթոդները enumerable: false են, ինչը նշանակում է՝ դրանք չեն երևում for..in ցիկլի ժամանակ։