当前位置: 代码迷 >> Web前端 >> with话语的本质
  详细解决方案

with话语的本质

热度:86   发布时间:2012-09-07 10:38:15.0
with语句的本质
通过测试及debug后, 得出结论, 仅代表个人意见:

with的本质, 就是构造一个作用域, 将with的对象的所有key, 声明为局部变量



var inner = '123';
var outer = {inner : 'hello world', another: 'hello with'};
window.obj = {outer: outer};

with(outer) {
	alert(inner);
	alert(another);
	alert(outer.inner);
	alert(window.obj.outer.inner);
	show();
}


function show() {
	alert(inner);

}


等同于以下代码


var inner = '123';
var outer = {inner : 'hello world', another: 'hello with'};
window.obj = {outer: outer};

(function(inner, another) {
	alert(inner);
	alert(another);
	alert(outer.inner);
	alert(window.obj.outer.inner);
	show();
})(outer.inner, outer.another);


function show() {
	alert(inner);

}






再看看下面这个常见的问题, 一目了然
var root = { 
    branch: { 
        node: 1 
    } 
}; 

with(root.branch) { 
    root.branch = { 
        node: 0 
    }; 
    // 显示 1, 错误! 
    alert(node); 
} 
// 显示 0, 正确! 
alert(root.branch.node); 



  相关解决方案