Classes, inheritance va Date obyekti

DarslarWeb dasturlash

Classes, inheritance va Date obyekti

ES6 class sintaksisi, constructor, extends, super. Vaqt bilan ishlash uchun Date obyekti.

55 daqiqa
JavaScript — Dars 14

Class, meros va Date

Prototip zanjirining toza, o'qilishi oson sintaksisi. Va vaqt bilan ishlash.

classextendssuperDate

Dars rejasi

6 ta mavzu
  1. 01class nima va nega kerak?
  2. 02constructor va metodlar
  3. 03extends — meros olish
  4. 04super — otaga murojaat
  5. 05static metodlar
  6. 06Date obyekti

1. class nima va nega kerak?

class — prototiplar ustidagi shakar sintaksis. Ichki ishlashi bir xil, lekin yozish/o'qish ancha oson.

Prototip bilan (eski)
1function User(ism, yosh) {
2 this.ism = ism;
3 this.yosh = yosh;
4}
5
6User.prototype.salom = function() {
7 return "Salom, " + this.ism;
8};
9
10const u = new User("Ali", 17);
class bilan (zamonaviy)
1class User {
2 constructor(ism, yosh) {
3 this.ism = ism;
4 this.yosh = yosh;
5 }
6
7 salom() {
8 return "Salom, " + this.ism;
9 }
10}
11
12const u = new User("Ali", 17);
13console.log(u.salom()); // "Salom, Ali"

2. constructor va metodlar

1class Mahsulot {
2 constructor(nom, narx, miqdor = 1) {
3 this.nom = nom;
4 this.narx = narx;
5 this.miqdor = miqdor;
6 }
7
8 // Metod
9 jami() {
10 return this.narx * this.miqdor;
11 }
12
13 // Ma'lumot chiqarish
14 tavsif() {
15 return `${this.nom} × ${this.miqdor} = ${this.jami()} so'm`;
16 }
17
18 // Qiymat o'zgartirish
19 chegirma(foiz) {
20 this.narx -= this.narx * foiz / 100;
21 }
22}
23
24const m = new Mahsulot("Olma", 5000, 3);
25console.log(m.jami()); // 15000
26console.log(m.tavsif()); // "Olma × 3 = 15000 so'm"
27m.chegirma(10);
28console.log(m.jami()); // 13500

3. extends — meros olish

1class Hayvon {
2 constructor(ism) {
3 this.ism = ism;
4 }
5 yur() {
6 return this.ism + " yurmoqda";
7 }
8}
9
10// It sinfi Hayvon'dan meros oladi
11class It extends Hayvon {
12 hurish() {
13 return this.ism + ": Vov-vov!";
14 }
15}
16
17const rex = new It("Rex");
18console.log(rex.yur()); // "Rex yurmoqda" (ota'dan)
19console.log(rex.hurish()); // "Rex: Vov-vov!" (o'zidan)

4. super — otaga murojaat

1class Hayvon {
2 constructor(ism) {
3 this.ism = ism;
4 }
5 tanishtir() {
6 return "Men " + this.ism;
7 }
8}
9
10class It extends Hayvon {
11 constructor(ism, zot) {
12 super(ism); // ota constructor'ini chaqirish
13 this.zot = zot;
14 }
15
16 tanishtir() {
17 const asosiy = super.tanishtir(); // ota metodiga murojaat
18 return asosiy + ", zotim — " + this.zot;
19 }
20}
21
22const rex = new It("Rex", "Labrador");
23console.log(rex.tanishtir());
24// "Men Rex, zotim — Labrador"

5. static metodlar

static metodlar obyekt emas, class'ning o'zida yashaydi. Yordamchi funksiyalar uchun qulay.

1class Matematika {
2 static qoshish(a, b) {
3 return a + b;
4 }
5
6 static kvadrat(x) {
7 return x * x;
8 }
9}
10
11// new kerak emas!
12Matematika.qoshish(5, 3) // 8
13Matematika.kvadrat(4) // 16
14
15// Taniqli misol
16Math.max(1, 2, 3) // Math'dagi hamma — static
17Array.isArray([1, 2]) // true

6. Date obyekti

1// Hozirgi vaqt
2const hozir = new Date();
3console.log(hozir);
4
5// Aniq sana — diqqat: oy 0 dan boshlanadi!
6const sana = new Date(2026, 3, 18); // 18-aprel 2026
7
8// String'dan
9const sana2 = new Date("2026-04-18");
10
11// Qismlarini olish
12hozir.getFullYear() // 2026
13hozir.getMonth() // 3 (aprel — 0 dan)
14hozir.getDate() // oyning kuni (1-31)
15hozir.getDay() // hafta kuni (0 = yakshanba)
16hozir.getHours() // soat
17hozir.getMinutes() // daqiqa
18hozir.getSeconds() // soniya
19
20// Vaqtni o'zgartirish
21hozir.setFullYear(2027);
22hozir.setDate(1);
23
24// Format qilish
25hozir.toLocaleDateString("uz-UZ") // "18.04.2026"
26hozir.toLocaleTimeString("uz-UZ") // "14:35:22"
27hozir.toISOString() // "2026-04-18T14:35:22.000Z"
28
29// Millisekund — 1970-yildan beri
30Date.now() // 1745000000000
31hozir.getTime() // o'sha son
Farq hisoblash
1const tugilgan = new Date("2008-05-20");
2const hozir = new Date();
3
4const farqMs = hozir - tugilgan;
5const kun = Math.floor(farqMs / (1000 * 60 * 60 * 24));
6const yil = Math.floor(kun / 365);
7
8console.log(`${yil} yosh, taxminan ${kun} kun yashadingiz`);
Bank hisobi (class bilan)
Natijajonli

Kichik test

0 / 4

1.class ichidagi constructor qachon chaqiriladi?

2.Bola class'da super() qaerda chaqirilishi shart?

3.static metodni qanday chaqiriladi?

4.Date'da oy qanday raqamlanadi?

Mustaqil ish

0 / 6