# JavaScript meros (extends, super)

> Klasslarda meros: extends bilan kengaytirish, super chaqiruvi, metodni qayta yozish va instanceof tekshiruvi. Kod takrorlanishini kamaytirishni o'rganing.

Manba: https://learncode.uz/javascript/meros/
Sana: 2026-06-04

Bir necha [klass](https://learncode.uz/javascript/klasslar) umumiy xususiyatga ega bo'lsa, ularni
har birida qayta yozish shart emas. **Meros** umumiy qismni bitta joyda
saqlashga imkon beradi.

## Muammo: takrorlanadigan klasslar

```js
class Student {
  constructor(name, age) { this.name = name; this.age = age; }
  introduce() { return this.name + ", " + this.age + " age"; }
}

class Teacher {
  constructor(name, age) { this.name = name; this.age = age; }
  introduce() { return this.name + ", " + this.age + " age"; }
}
```

Ikkalasida ham `name`, `age` va `introduce` bir xil. Umumiy qismni ajratish
kerak.

## extends — kengaytirish

```js
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  introduce() {
    return this.name + ", " + this.age + " age";
  }
}

class Student extends Person {
  constructor(name, age, group) {
    super(name, age);
    this.group = group;
  }
}

const aziz = new Student("Aziz", 20, "IT-21");

console.log(aziz.introduce());
console.log(aziz.group);
```

`Student` `introduce` metodini yozmagan, lekin u ishladi — ota klassdan
**meros** qilib olindi.

> **Atama**
>
> **Meros** — bir klassning boshqa klass xossalari va metodlarini olishi.
> `extends` bilan **avlod** (child) klass **ota** (parent) klassni kengaytiradi.
> **`super`** — ota klassga murojaat: constructor'da uning constructor'ini,
> metodda uning metodini chaqiradi.

## super majburiy

Avlod constructor'ida `this` dan **oldin** `super()` chaqirilishi shart:

```js
class Person {
  constructor(name) {
    this.name = name;
  }
}

class Student extends Person {
  constructor(name, group) {
    try {
      this.group = group;
    } catch (error) {
      console.log("Xato:", error.message);
    }
    super(name);
    this.group = group;
  }
}

const t = new Student("Aziz", "IT-21");
console.log(t.name, t.group);
```

Sababi: `this` obyekti ota constructor tomonidan yaratiladi — u chaqirilmaguncha
`this` mavjud emas.

## Metodni qayta yozish

Avlod ota metodini o'zinikiga almashtira oladi:

```js
class Person {
  constructor(name) {
    this.name = name;
  }
  introduce() {
    return "Men " + this.name;
  }
}

class Teacher extends Person {
  constructor(name, subject) {
    super(name);
    this.subject = subject;
  }

  introduce() {
    return super.introduce() + ", " + this.subject + " o'qituvchisi";
  }
}

const person = new Person("Aziz");
const teacher = new Teacher("Dilnoza", "MathUtils");

console.log(person.introduce());
console.log(teacher.introduce());
```

`super.tanishtir()` ota versiyasini chaqirdi, so'ng natijaga qo'shimcha
qo'shildi. Bu — noldan yozishdan ko'ra yaxshi usul.

## Ko'p bosqichli meros

```js
class Vehicle {
  constructor(title) {
    this.title = title;
  }
  move() {
    return this.title + " harakatlanmoqda";
  }
}

class Car extends Vehicle {
  constructor(title, wheels) {
    super(title);
    this.wheels = wheels;
  }
  move() {
    return super.move() + " (" + this.wheels + " g'ildirak)";
  }
}

class Truck extends Car {
  constructor(title, wheels, capacity) {
    super(title, wheels);
    this.capacity = capacity;
  }
  move() {
    return super.move() + ", " + this.capacity + " tonna yuk bilan";
  }
}

const y = new Truck("KamAZ", 6, 12);
console.log(y.move());
```

Zanjir bo'ylab har daraja o'z hissasini qo'shdi.

## instanceof — turini tekshirish

```js
class Person {}
class Student extends Person {}
class Teacher extends Person {}

const t = new Student();

console.log(t instanceof Student);
console.log(t instanceof Person);
console.log(t instanceof Teacher);
```

Obyekt **butun zanjirga** tegishli hisoblanadi: `Student` ham `Person`.

## Amaliy misol — xato klasslari

Meros [xatolarni](https://learncode.uz/javascript/xatolar) turkumlashda ko'p ishlatiladi:

```js
class AppError extends Error {
  constructor(message, kod) {
    super(message);
    this.name = "AppError";
    this.kod = kod;
  }
}

class NotFoundError extends AppError {
  constructor(nima) {
    super(nima + " topilmadi", 404);
    this.name = "NotFoundError";
  }
}

function find(title) {
  if (title !== "Aziz") throw new NotFoundError("Student");
  return { name: title };
}

try {
  find("Kimdir");
} catch (error) {
  console.log(error.name);
  console.log(error.message);
  console.log(error.kod);
  console.log(error instanceof AppError);
  console.log(error instanceof Error);
}
```

`catch` blokida `instanceof` bilan xato turiga qarab turlicha ish tutish
mumkin.

## Merosdan foydalanmaslik ham to'g'ri

> **Chuqur meros — muammo**
>
> Uch-to'rt darajali meros zanjiri kodni tushunishni qiyinlashtiradi: metod
> qayerdan kelganini topish uchun butun zanjirni ko'rish kerak bo'ladi.
>
> Ko'p hollarda **kompozitsiya** (obyektni ichiga qo'yish) merosdan yaxshiroq:
> `Car` `Dvigatel` dan meros olmaydi — uni **ichida saqlaydi**.

## Amaliy mashq

1. `Animal` klassini yozing: `name` va `sound()` metodi;
2. `Dog` va `Cat` klasslarini `extends` bilan yarating;
3. har birida `sound()` ni qayta yozing;
4. avlod constructor'iga qo'shimcha xossa qo'shing va `super` chaqiring;
5. `super.ovoz()` ni chaqirib, natijaga qo'shimcha qo'shing;
6. `instanceof` bilan uch xil tekshiruv o'tkazing.

## Qisqacha xulosa

- `extends` avlod klassni ota klass asosida yaratadi.
- Avlod constructor'ida `super()` **`this` dan oldin** chaqirilishi shart.
- Metod qayta yozilsa, ota versiyasi `super.metod()` bilan chaqiriladi.
- `instanceof` butun meros zanjirini hisobga oladi.
- `Error` dan meros olib, o'z xato turlaringizni yaratish mumkin.
- Chuqur meros o'rniga ko'pincha kompozitsiya afzal.

Keyingi darsda [Set va Map](https://learncode.uz/javascript/set-map) — maxsus to'plam turlari.
