日期:2014-05-16 浏览次数:20364 次
?
?
1、全局变量
?
?
var scope = "global";
function f() {
alert(scope); // global
}
f();
?
2、局部变量被 hoisted
?
?
var scope = "global";
function f() {
alert(scope); // undefined
var scope = "local";
}
f();
?
?3、强制访问外部域
?
?
var scope = "global";
function f() {
alert(this.scope); // global
var scope = "local";
}
f();
?
4、外部变量被隐藏
?
?
var scope = "global";
function f() {
var scope = "local";
alert(scope); // local
}
f();
?
?
5、外部变量被修改
?
?
var scope = "global";
function f() {
scope = "local";
alert(scope); // local
}
f();
alert(scope); // local
?
?? 相当于
?
?
var scope = "global";
function f() {
this.scope = "local";
alert(scope); // local
}
f();
alert(scope); // local
?
6、函数被当成 构造方法使用
?
?
var scope = "global";
function f() {
this.scope = "local"; // 这里的this 变了
alert(scope); // global
}
f1 = new f(); // f 被当成 constructor 使用
alert(f1.scope); // local
alert(scope); // global
?
?