开发者

Should I use the "with" statement in generated code?

开发者 https://www.devze.com 2023-04-01 03:21 出处:网络
I am developing a programming language which compiles to javascript, the code generated contains too much repetition, like:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号