function foo() {
x = 10;
}
foo();
console.log(x); // 10....??????????
암묵적 전역 : 변수 선언이 존재하지 않으면, JS는 전역 객체에 해당 변수와 동일하게 사용할 수 있는 프로퍼티를 동적 생성한다🤬
'use strict';
function foo() {
x = 10; // ReferenceError: x is not defined😀
}
foo();
function foo() {
'use strict';
x = 10; // ReferenceError: x is not defined
}
foo();
(function () {
'use strict';
function foo() {
x = 10; // ReferenceError: x is not defined
}
foo();
}());
(function() {
'use strict';
x = 1; // ReferenceError: x is not defined
console.log(x);
}());
(function() {
'use strict';
var x = 1;
delete x; // SyntaxError: Delete of an unqualified identifier in strict mode.
function foo(a) {
delete a; // SyntaxError: Delete of an unqualified identifier in strict mode.
}
delete foo; // SyntaxError: Delete of an unqualified identifier in strict mode.
}());
(function() {
'use strict';
function foo(x, x) { // SyntaxError: Duplicate parameter name not allowed in this context
return x * x;
}
}());
(function() {
'use strict';
// SyntaxError: Strict mode code may not include a with statement
with ({ x: 1 }) {
console.log(x);
}
}());
// 아래와 동일
with(console) { // document 생략
log("111");
log("222");
log("333");
}
// 위와 동일
console.log("111");
console.log("222");
console.log("333");
(function() {
'use strict';
function foo() {
console.log(this);
}
foo(); // undefined
function Foo() {
console.log(this);
}
new Foo(); // Foo
}());
(function(a) {
'use strict';
a = 2;
console.log(a); // 2
console.log(arguments); // { 0: 1, length: 1}
}(1));