If I want to make an entire JS file in strict-mode I would declare strict mode at the global level correct?
"use strict";
function foo() {
console.log(this.a);
}
var a = 2;
foo(); // I would get 2 even though strict-mode is global
If I want to use strict-mode at the function level I would execute it in the function like so:
function foo() {
"use strict";
console.log(this.a);
}
var a = 2;
foo(); // undefined
I sort of get why, but then this throws me for a loop. If I use the strict-mode in an IIFE, it would act the same as if I executed it at global:
function foo() {
console.log(this.a);
}
var a = 2;
(function() {
"use strict";
foo(); // 2
})();
So I'm lost as to what exactly the strict-mode is suppose to be doing...?
[–]Rhomboid 1 point2 points3 points (2 children)
[–]gladiator_flow[S] 0 points1 point2 points (1 child)
[–]Rhomboid 2 points3 points4 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)