我应该使用“with”吗?生成代码中的语句?
我正在开发一种编译为 javascript 的编程语言,生成的代码包含太多重复,例如:
cls.$init = function(){
this.property1 = {};
this.anotherProperty = [1, 2, 3, 4];
this.yetAnotherProperty = "test";
/* etc */
}
使用 with
语句可以将其变得更小(在这种情况下,在初始化许多属性时):
cls.$init = function(){
with(this){
property1 = {};
anotherProperty = [1, 2, 3, 4];
yetAnotherProperty = "test";
/* etc */
}
}
但问题是......我应该在生成的代码中使用 with
语句吗? (这不是为了以后修改)
I am developing a programming language which compiles to javascript, the code generated contains too much repetition, like:
cls.$init = function(){
this.property1 = {};
this.anotherProperty = [1, 2, 3, 4];
this.yetAnotherProperty = "test";
/* etc */
}
This could be made much smaller (in that case, when initializing many properties), using a with
statement:
cls.$init = function(){
with(this){
property1 = {};
anotherProperty = [1, 2, 3, 4];
yetAnotherProperty = "test";
/* etc */
}
}
But the question is... should I use with
statements in generated code? (Which is not meant to be modified later)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用严格模式时,
with
语句将在下一个 ECMAScript 标准中消失,因此我会习惯不使用它。https://developer.mozilla.org/en/JavaScript/Strict_mode#Simplifying_variable_uses
The
with
statement is going away in the next ECMAScript standard when using strict mode, so I would get used to not using it.https://developer.mozilla.org/en/JavaScript/Strict_mode#Simplifying_variable_uses
为什么您担心自动生成的代码中的重复?当 gzip 压缩时,它可能会被压缩,并且添加
with
会在运行时产生开销。 Douglas Crockford 还表示它将消失:http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/#comment-586082Why are you worried about repetition in auto-generated code? It will likely be compressed away when gzipped and adding a
with
incurs overhead at runtime. Douglas Crockford also says it is going away: http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/#comment-586082