+ 2

Strict Mode in Javascript.

when i use the strict mode the output is false but without strict mode the output is true why? "use strict"; function myFun(){ console.log(this==window);//it refers to the window object } myFun();

15th Jul 2020, 1:43 AM
Samir Singh
Samir Singh - avatar
1 ответ
+ 5
When strict mode is enabled "this" points to an undefined value if used within a function. undefined == window //false Such behavior prevents the silent creation of global variables this way: function bad(){ this.sth = 42; //now sth is global ..... //more code } var object = bad(); //You shouldn't call a constructor without the "new" keyword. Why? See line below↓ console.log(sth); //42 oops When strict mode is enabled you will be forced to use the new keyword. That's a good practice, strict mode enabled or not. Hope this helps.
16th Jul 2020, 1:14 AM
Kevin ★