这是一个相关的答案:
JavaScript 的“with”语句是否有合法用途?
另外:http: //yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
以下作为替代方案怎么样?
(function(obj) {
obj.y = obj.myFunc(obj.x);
obj.z = 6;
obj.otherFunc();
obj.a = {a:1,b:2,c:obj.processThree(3)};
})(myAwkwardlyNamedObjectToBeUsedAsANameSpaceThatIWishToModifyAndNotJustRead);
编辑:为了清晰和后代,这也是可能的(并且在大多数情况下比上述更可取——我不建议在全局范围内执行此操作,但我不建议在全局范围内执行任何操作):
function someFunction() {
// in some function
var obj = myAwkwardlyNamedObjectToBeUsedAsANameSpaceThatIWishToModifyAndNotJustRead;
obj.y = obj.myFunc(obj.x);
obj.z = 6;
obj.otherFunc();
obj.a = {a:1,b:2,c:obj.processThree(3)};
// ... any other stuff in the function
}