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 mavzu1. 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 // Metod9 jami() {10 return this.narx * this.miqdor;11 }12
13 // Ma'lumot chiqarish14 tavsif() {15 return `${this.nom} × ${this.miqdor} = ${this.jami()} so'm`;16 }17
18 // Qiymat o'zgartirish19 chegirma(foiz) {20 this.narx -= this.narx * foiz / 100;21 }22}23
24const m = new Mahsulot("Olma", 5000, 3);25console.log(m.jami()); // 1500026console.log(m.tavsif()); // "Olma × 3 = 15000 so'm"27m.chegirma(10);28console.log(m.jami()); // 135003. 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 oladi11class 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 chaqirish13 this.zot = zot;14 }15
16 tanishtir() {17 const asosiy = super.tanishtir(); // ota metodiga murojaat18 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) // 813Matematika.kvadrat(4) // 1614
15// Taniqli misol16Math.max(1, 2, 3) // Math'dagi hamma — static17Array.isArray([1, 2]) // true6. Date obyekti
1// Hozirgi vaqt2const hozir = new Date();3console.log(hozir);4
5// Aniq sana — diqqat: oy 0 dan boshlanadi!6const sana = new Date(2026, 3, 18); // 18-aprel 20267
8// String'dan9const sana2 = new Date("2026-04-18");10
11// Qismlarini olish12hozir.getFullYear() // 202613hozir.getMonth() // 3 (aprel — 0 dan)14hozir.getDate() // oyning kuni (1-31)15hozir.getDay() // hafta kuni (0 = yakshanba)16hozir.getHours() // soat17hozir.getMinutes() // daqiqa18hozir.getSeconds() // soniya19
20// Vaqtni o'zgartirish21hozir.setFullYear(2027);22hozir.setDate(1);23
24// Format qilish25hozir.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 beri30Date.now() // 174500000000031hozir.getTime() // o'sha sonFarq 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 / 41.class ichidagi constructor qachon chaqiriladi?
2.Bola class'da super() qaerda chaqirilishi shart?
3.static metodni qanday chaqiriladi?
4.Date'da oy qanday raqamlanadi?