24. Strict Mode - 安全なプログラミング
function foo() {
var a = 1;
b = 2; // global scope
}
function bar() {
”use strict”;
var c = 3;
d = 4; // Error
}
2010/02/19 24
27. with で疑似ブロックスコープ ×
window.alert(1);
with (window) {
alert(2);
for (var i = 0; i < 100; i++) {
with({i: i}) {
i = 0;
}
}
}
2010/02/19 27
28. with で疑似ブロックスコープ → NG
window.alert(1);
with (window) {
alert(2);
for (var i = 0; i < 100; i++) {
“use strict”;
with({i: i}) {
i = 0;
}
}
}
2010/02/19 28
30. 暗黙の this が global object でなくなる
// no strict
function Point(x, y) {
this.x = x + 0.0;
this.y = y + 0.0;
}
var a = new Point(1, 2); // ok
var b = Point(1, 2); // miss
// b = {window.x = 1, window.y = 2};
2010/02/19 30
31. 暗黙の this が global object でなくなる
”use strict”;
function Point(x, y) {
this.x = x + 0.0;
this.y = y + 0.0;
}
var a = new Point(1, 2); // ok
var b = Point(1, 2); // miss
// b = {undefined.x = 1, undefined.y = 2};
2010/02/19 31