我应该使用“with”吗?生成代码中的语句?

发布于 2024-12-02 12:57:41 字数 538 浏览 0 评论 0原文

我正在开发一种编译为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-12-09 12:57:41

使用严格模式时,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

清音悠歌 2024-12-09 12:57:41

为什么您担心自动生成的代码中的重复?当 gzip 压缩时,它可能会被压缩,并且添加 with 会在运行时产生开销。 Douglas Crockford 还表示它将消失:http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/#comment-586082

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文