Should I use the "with" statement in generated code?
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];
yetAnotherPro开发者_JS百科perty = "test";
/* etc */
}
}
But the question is... should I use with
statements in generated code? (Which is not meant to be modified later)
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
Why 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
精彩评论