What is 'use-strict' in JavaScript?

နောက်ထပ် Page မှာ အမေးများတဲ့ မေးခွန်းတစ်ခုဖြစ်တဲ့ "use-strict" ဆိုတာဘာလဲဆိုတာကို ပြောပြသွားမှာဖြစ်ပါတယ်။

"use strict"

"use strict";

"use strict" ဆိုတာ JavaScript ရဲ့ "Strict Mode" ကိုသုံးဖို့ invoke လုပ်တဲ့ expression ဖြစ်ပါတယ်။

Strict Mode

Strict Mode ဆိုတာကတော့ Code ကို ပိုပြီး secure ဖြစ်အောင် ၊ reliable ဖြစ်အောင် semantics အားဖြင့် ပိုပြီးတင်းကျပ်ထားတဲ့ JavaScript variant တစ်မျိုးဖြစ်ပါတယ်။ ES5 မှာမှ ပါဝင်လာတာဖြစ်ပါတယ်။ ES5 မတိုင်ခင်က JavaScript မှာ မကောင်းတဲ့ Language Feature လို့ပဲပြောရမလား ၊ အဲလိုတွေရှိခဲ့ပါတယ်။ အများစုကတော့ Developer ရဲ့အမှားကို Language က ထောက်ပြတာမျိုးမရှိပဲ Silent ဖြစ်နေတာပါ။

Developer တွေအများဆုံးမှားလေ့ရှိတာကတော့ Syntax Error တွေပဲဖြစ်ပါတယ်။ အရင် ES5 မတိုင်ခင် JavaScript က ဒီလို Syntactical Error တွေကို မပြောပြပါဘူး။ ဆိုတော့ Code ကြီးက အမှားတွေနဲ့လည်း run နေနိုင်ပါတယ်။ Result က ကိုယ်လိုချင်သလို (Expected) ဖြစ်ချင်မှဖြစ််နေပါလိမ့်မယ်။ ပြီးတော့ Debugging လုပ်တဲ့အချိန်မှာလည်း ဒီလို အမှားတွေကို ရှာရတာဟာ ခက်ခဲပါတယ်။

Strict Mode ကလုပ်ပေးတာဟာ အဓိကအားဖြင့်တော့ သုံးမျိုးရှိပါတယ်။

  • Silent ဖြစ်နေတဲ့အမှားတွေကို Throw ပေးတာ
  • ပိုပြီး Secure ဖြစ်အောင် လုပ်ပေးတာ
  • Code ကို Optimization လုပ်ပေးတာ

How to enable Strict Mode

အပေါ်မှာပြောခဲ့သလိုပဲ 'use strict' ဆိုတဲ့ expression နဲ့ enable လုပ်ပါတယ်။

'use strict';

// the rest of the code in the file
...

Script File တစ် file ရဲ့ အပေါ်ဆုံးကနေပြီးတော့ တစ် File လုံး Strict Mode ကိုသုံးဖို့ ပြောနိုင်ပါတယ်။

function stricty() {
    'use strict';

    // the rest of the code in the function
    ...
}

Function တစ်ခုချင်းအတွက်ပဲလည်း Strict Mode ကို enable လုပ်လို့ရပါတယ်။

ES5 Modules တွေကလည်း Strict Mode ကို Default enable လုပ်ပြီးသားဖြစ်ပါတယ်။

Strict Mode နဲ့ Non Strict Mode ဘာတွေကွာလဲ

'use strict';

misTypeVariable = 69;

ပုံမှန်ဆိုရင် Variable ကိုမှားရေးမိတဲ့အချိန်မှာ JavaScript ဟာ Global Variable အသစ်တစ်ခု Create သွားပါတယ်။ Strict Mode မှာဆိုရင်တော့ ဒီလို Mistyping ကြောင့် Global Variable တွေထွက်လာမှာမဟုတ်ပဲ အောက်ကလို Error တက်မှာဖြစ်ပါတယ်။

misTypeVariable = 69;
                ^

ReferenceError: misTypeVariable is not defined

function sum(a, a, c) {
  'use strict';
  return a + a + c;
}

အပေါ်က Function ကိုကြည့်ရင် နာမည်တူနေတဲ့ Parameter ရှိနေတာကိုတွေ့မှာပါ။ Strict Mode မှာ ဒါမျိုးလုပ်လို့မရပါဘူး။

function sum(a, a, c) {
                ^

SyntaxError: Duplicate parameter name not allowed in this context

'use strict';

var a = 45;
delete a;

Variable ကိုလည်း Strict Mode မှာ Delete လို့ရမှာ မဟုတ်ပါဘူး။

delete a;
       ^

SyntaxError: Delete of an unqualified identifier in strict mode.

'use strict';

var obj = {};
Object.defineProperty(obj, 'x', { value: 42, writable: false });
obj.x = 9;

Readonly ဖြစ်တဲ့ Property တွေကို Assign လုပ်လို့လည်းမရတော့ပါဘူး။

obj.x = 9; // throws a TypeError
       ^

TypeError: Cannot assign to read only property 'x' of object '#<Object>'
'use strict';

var obj = {
    get x() {
        return 17;
    }
};
obj.x = 777;

Getter ပဲရှိတဲ့ Property တွေကိုလည်း Assign လုပ်ခွင့်မရှိပါဘူး။

obj.x = 777; // throws a TypeError
       ^

TypeError: Cannot set property x of #<Object> which has only a getter

'use strict'

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

နောက်ပြီးတော့ Function ထဲကနေ Browser ရဲ့ window object ကိုလည်း reference ယူလို့ရတော့မှာမဟုတ်ပါဘူး။

References