Javascript javascript中的“with”关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1931186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"with" keyword in javascript
提问by John McCollum
Possible Duplicate:
Are there legitimate uses for JavaScript’s “with” statement?
I recently discovered that in javascript, one can do something like the following:
我最近发现在 javascript 中,可以执行以下操作:
with document{
write('foo');
body.scrollTop = x;
}
The down side of this is that each variable needs to be checked to see if it belongs to the document object, creating a significant overhead.
这样做的缺点是需要检查每个变量以查看它是否属于文档对象,从而产生大量开销。
Alternatively, one could do something like this:
或者,你可以做这样的事情:
var d = document;
d.write('foo');
d.body.scrollTop = x;
Are there any situations where the use of the 'with' keyword is justified?
是否有任何情况下使用 'with' 关键字是合理的?
回答by azazul
Just don't use it: http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
只是不要使用它:http: //yuiblog.com/blog/2006/04/11/with-statement-thinked-harmful/
JavaScript's
withstatement was intended to provide a shorthand for writing recurring accesses to objects. So instead of writingooo.eee.oo.ah_ah.ting.tang.walla.walla.bing = true; ooo.eee.oo.ah_ah.ting.tang.walla.walla.bang = true;You can write
with (ooo.eee.oo.ah_ah.ting.tang.walla.walla) { bing = true; bang = true; }That looks a lot nicer. Except for one thing. There is no way that you can tell by looking at the code which
bingandbangwill get modifed. Willooo.eee.oo.ah_ah.ting.tang.walla.wallabe modified? Or will the global variablesbingandbangget clobbered? It is impossible to know for sure...If you can't read a program and be confident that you know what it is going to do, you can't have confidence that it is going to work correctly. For this reason, the
withstatement should be avoided...
JavaScript 的
with声明旨在为编写对对象的重复访问提供一种简写。所以而不是写作ooo.eee.oo.ah_ah.ting.tang.walla.walla.bing = true; ooo.eee.oo.ah_ah.ting.tang.walla.walla.bang = true;你可以写
with (ooo.eee.oo.ah_ah.ting.tang.walla.walla) { bing = true; bang = true; }那看起来好多了。除了一件事。没有办法,你可以通过查看该代码告诉
bing并且bang将得到修饰的。会ooo.eee.oo.ah_ah.ting.tang.walla.walla被修改吗?或将全局变量bing和bang得到打一顿?不可能确切知道......如果您无法阅读程序并确信您知道它要做什么,那么您就无法确信它会正确运行。因此,
with应避免使用该语句...
回答by Rich
Despite advice to the contrary almost everywhere, I think that there are uses for "with". For example, I'm working on a domain model framework for Javascript, which uses the underscore character in much the same way that jQuery uses "$". This means that without "with", I have lots of underscores scattered through my code in ways that make it less readable. Here's a random line from an application using the framework:
尽管几乎到处都有相反的建议,但我认为“with”是有用途的。例如,我正在为 Javascript 开发一个域模型框架,它使用下划线字符的方式与 jQuery 使用“$”的方式非常相似。这意味着如果没有“with”,我的代码中有很多下划线以降低可读性的方式分散在我的代码中。这是使用该框架的应用程序的随机行:
_.People().sort(_.score(_.isa(_.Parent)),'Surname','Forename');
whereas with "with" it would look like
而使用“with”,它看起来像
with (_) {
...
People().sort(score(isa(Parent)),'Surname','Forename');
...
}
What would be really useful is a read-only version of "with".
真正有用的是“with”的只读版本。
回答by meder omuraliev
I would avoid using it in production code because it's ambiguous but there is an alternative solution to the for-loop-closure solution by using withto mimic the letbinding, here's a copy of my previous answer:
我会避免在生产代码中使用它,因为它是模棱两可的,但是通过使用with来模拟let绑定,有一个替代 for-loop-closure 解决方案的解决方案,这是我之前答案的副本:
An alternative to the standard closure solution using functions inside of a for loop:
使用 for 循环内的函数替代标准闭包解决方案:
<a href="#">blah</a><br>
<a href="#">blah</a><br>
<a href="#">foo</a><br>
<script>
(function() {
var anchors = document.getElementsByTagName('a');
for ( var i = anchors.length; i--; ) {
var link = anchors[i];
with ({ number: i }) {
link.onclick = function() {
alert(number);
};
}
}
})();
</script>
Credit to nlogaxfor providing a solution which I pretty much ripped off:
Javascript infamous Loop issue?
感谢nlogax提供我几乎扯下一个解决方案:
使用Javascript臭名昭著的循环问题?
Here's the standard solution:
这是标准解决方案:
<script>
(function() {
var anchors = document.getElementsByTagName('a');
for ( var i = anchors.length; i--; ) {
var link = anchors[i];
(function(i) {
link.onclick = function() {
alert(i)
}
})(i);
}
})();
</script>

