Seriya: JavaScript Basics Dars 9

Strict Mode

JavaScript'da Strict Mode nima, qanday ishlashi va nimalarni o'zgartirishini o'rganing.

9-dars

🧠 Strict Mode nima?

"use strict";

Bu yozilsa:

  • JS eski, xavfli, noaniq xatti-harakatlarni taqiqlaydi
  • JS'ni "intizomli" qiladi

🎯 Nega kerak?

Normal JS juda yumshoq (loose). Masalan:

x = 10;
console.log(x);

Bu ishlaydi ❗

Lekin x e'lon qilinmagan. Bu global obyektga yoziladi → xavfli.

Strict mode'da:

"use strict";

x = 10; // ❌ ReferenceError

Endi JS ruxsat bermaydi.

🔥 Strict Mode nimani o'zgartiradi?

1️⃣ E'lon qilinmagan variable taqiqlanadi

"use strict";

a = 5; // ❌ error

2️⃣ Duplicate parameter taqiqlanadi

Normal:

function test(a, a) {
  return a;
}

Strict mode'da:

"use strict";

function test(a, a) {} // ❌ error

3️⃣ this o'zgaradi

Normal mode:

function test() {
  console.log(this);
}

test(); // window (browserda)

Strict mode:

"use strict";

function test() {
  console.log(this);
}

test(); // undefined

Bu juda muhim.

4️⃣ O'chirilmaydigan property'ni delete qilib bo'lmaydi

"use strict";

delete Object.prototype; // ❌ error

🧠 Eng muhim o'zgarish — this

Strict mode'da:

  • Global function ichida this → undefined
  • Avval esa window edi

Bu xatoliklarni tez topishga yordam beradi.

🧩 Strict Mode qayerda ishlaydi?

1️⃣ File boshida

"use strict";

// Butun fayl strict bo'ladi

2️⃣ Faqat function ichida

function test() {
  "use strict";
  // Faqat shu function strict
}

3️⃣ ES Modules'da Strict avtomatik

// ES module file
export function test() {}

ESM har doim strict mode'da ishlaydi. Shuning uchun zamonaviy JS'da ko'p joyda "use strict" yozilmaydi.

📊 Xulosa

Joy Strict qachon
File boshi Butun fayl strict
Function ichi Faqat shu function strict
ES Modules Avtomatik strict

Strict mode — bu:

  • Silent bug'larni error'ga aylantiradi
  • JS'ni predictable qiladi
  • Production code'da doim ishlatiladi

💡 Amaliy maslahatlar

  1. Yangi loyihalarda — ES Modules ishlating, strict avtomatik bo'ladi
  2. E'lon qilinmagan o'zgaruvchi — strict mode xatolik beradi, tez topasiz
  3. this — strict mode'da global function'da undefined, window ga tayanmaslik uchun yaxshi
  4. Duplicate parameter — strict mode taqiqlaydi, kod sifati yaxshilanadi
  5. delete — faqat o'chiriladigan property'larga ishlating

🔍 Qo'shimcha misollar

// Normal mode — ishlaydi (xavfli)
x = 10;
console.log(x); // 10

// Strict mode — ReferenceError
"use strict";
y = 10; // ReferenceError: y is not defined

// Duplicate parameter
"use strict";
function sum(a, a) {} // SyntaxError: Duplicate parameter name not allowed

// this farqi
function showThis() {
  console.log(this);
}
showThis(); // window (browser) yoki global (Node)

"use strict";
function showThisStrict() {
  console.log(this);
}
showThisStrict(); // undefined

// ES Module — strict avtomatik
// myModule.js
export const value = 42;
// Bu fayl allaqachon strict mode'da

📝 Xulosa

  • "use strict" — JS'ni intizomli qiladi, eski xatti-harakatlarni taqiqlaydi
  • E'lon qilinmagan o'zgaruvchi → ReferenceError
  • Duplicate parameter → SyntaxError
  • Global function'da this → undefined (browserda window o'rniga)
  • ES Modules har doim strict — zamonaviy kodda alohida yozish shart emas
  • Production'da strict mode ishlatish — silent bug'larni oldini oladi