gpt4 book ai didi

javascript - 我应该在生成的代码中使用 "with"语句吗?

转载 作者:行者123 更新时间:2023-11-28 11:13:52 25 4
gpt4 key购买 nike

我正在开发一种编译为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 语句吗? (这并不意味着以后可以修改)

最佳答案

使用严格模式时,with 语句将在下一个 ECMAScript 标准中消失,因此我会习惯不使用它。

https://developer.mozilla.org/en/JavaScript/Strict_mode#Simplifying_variable_uses

关于javascript - 我应该在生成的代码中使用 "with"语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7274260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com