var obj={
x:1,
xyz:function(){
with(this){
function con(){
console.log(x);
console.log(this.x);
}
var x=2;//因为是在obj对象上下文中执行的,所以这里对x的初始化,覆盖了上面的x:1;
(function(){
con();
console.log(this==window)//true;
})();
con.call(this);
}
}
}
obj.xyz();//2,undefined,2,2
with语句将with包含的代码在this表示的上下文中执行,这里的this==obj;但是匿名函数中改变了this的指向,匿名函数中的this总是指向window,除了用一个变量将this保存起来,然后再匿名函数中使用。call(this)//这里的this是指obj;