JS Proxy
Proxy obyekti
Proxy — quyi darajadagi metaprogrammalash uchun metodlarga ega JavaScript obyekti.
Metaprogrammalash siz uchun yangimi?
Metaprogrammalash bo‘yicha darsligimizni ko‘ring
Proxy siz uchun yangimi?
Proxy sintaksisi
const proxy = new Proxy(target, handler);
- target - asl obyekt yoki funksiya
- handler - trap metodlariga ega obyekt
To‘liq Proxy handler ma’lumotnomasi
2025-yil dekabr oyida qayta ko‘rib chiqilgan
| Metod | Tavsif |
|---|---|
| handler.apply() | Funksiyani chaqirish |
| handler.construct() | Obyekt yaratish (new kabi) |
| handler.defineProperty() | Xususiyatni aniqlash (define) |
| handler.deleteProperty() | Xususiyatni o‘chirish |
| handler.get() | Xususiyatni olish |
| handler.getOwnPropertyDescriptor() | Xususiyat deskriptorini olish |
| handler.getPrototypeOf() | Obyekt prototipini olish |
| handler.has() | Xususiyat mavjudligini tekshirish |
| handler.isExtensible() | Obyekt kengaya olishini tekshirish |
| handler.ownKeys() | Barcha kalitlarni olish (symbol’lar bilan birga) |
| handler.preventExtensions() | Obyekt kengayishini to‘xtatish |
| handler.set() | Xususiyatni o‘rnatish |
| handler.setPrototypeOf() | Obyekt prototipini o‘rnatish |
Proxy trap’lari
Trap — Proxy handler ichidagi funksiya. U Proxy ustida ma’lum bir amal bajarilganda ishga tushadi.
Quyida har bir JavaScript Proxy trap’i va ularni nima ishga tushirishi to‘liq tushuntirilgan.
| Trap nomi | Qachon ishga tushadi |
|---|---|
| construct | Obyekt yaratilganda (new bilan) |
| apply | Funksiya chaqirilganda |
| get | Xususiyat o‘qilganda |
| set | Xususiyat o‘zgartirilganda |
| has | in operatori ishlatilganda |
| deleteProperty | Xususiyat o‘chirilganda |
| defineProperty | Xususiyat aniqlanganda |
| getOwnPropertyDescriptor | Xususiyat deskriptori olinganda |
| ownKeys | Xususiyatlar sanab chiqilganda |
| getPrototypeOf | Prototip olinganda |
| setPrototypeOf | Prototip o‘rnatilganda |
| isExtensible | Kengaytirilish imkoniyati tekshirilganda |
| preventExtensions | Kengaytirilish taqiqlanganda |
JavaScript ichki metodlari
Barcha JavaScript obyektlari 13 ta ichki metodga ega.
Har bir metodning o‘ziga mos Proxy trap’i bor:
| Ichki amal | Proxy trap’i |
|---|---|
| [[Construct]] | construct() |
| [[Call]] | apply() |
| [[Get]] | get() |
| [[Set]] | set() |
| [[HasProperty]] | has() |
| [[Delete]] | deleteProperty() |
| [[DefineProperty]] | defineProperty() |
| [[GetOwnProperty]] | getOwnProperty() |
| [[OwnPropertyKeys]] | ownPropertyKeys() |
| [[IsExstensible]] | isExtensible() |
| [[PreventExstensions]] | preventExtensions() |
| [[GetPrototypeOf]] | getPrototypeOf() |
| [[SetPrototypeOf]] | setPrototypeOf() |
Eslatma
Har bir Proxy trap’iga mos Reflect metodi bor — u trap bilan bir xil nom va signaturaga ega.
| Ichki amal | Reflect metodi |
|---|---|
| [[Construct]] | Reflect.construct() |
| [[Call]] | Reflect.apply() |
| [[Get]] | Reflect.get() |
| [[Set]] | Reflect.set() |
| [[HasProperty]] | Reflect.has() |
| [[Delete]] | Reflect.deleteProperty() |
| [[DefineProperty]] | Reflect.defineProperty() |
| [[GetOwnProperty]] | Reflect.getOwnProperty() |
| [[OwnPropertyKeys]] | Reflect.ownPropertyKeys() |
| [[IsExstensible]] | Reflect.isExtensible() |
| [[PreventExstensions]] | Reflect.preventExtensions() |
| [[GetPrototypeOf]] | Reflect.getPrototypeOf() |
| [[SetPrototypeOf]] | Reflect.setPrototypeOf() |
Shuningdek qarang...
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
