准备脚本以在 JavaScript 编译器中编译(Google Closure)
我在很多地方都写了代码。出于性能和语法检查的原因,我想重构代码以便能够使用 JavaScript 编译器。准备编译代码的建议是什么?
- 我用 EJS 编写了 JavaScript 代码,以在 JavaScript 中呈现一些服务器端数据:
代码:
<% if(product.type){ %>
$('#someid').html('<%= product.type %>'); //javascript
<% } %>
- 我认为编译器会更改我的变量名称(我知道可以将其设置为不更改哪个变量,但我在其中使用了很多变量页面、脚本或小部件/子页面;诸如全局
var
之类的东西),在这种情况下,它肯定会崩溃我的代码。
我听说以闭包风格编写有助于每个脚本文件之间的变量冲突。我是否必须使用该模式重写所有代码?我必须从 scatch 构建代码吗?我正在询问准备要编译的代码的方法,有什么建议吗?
I have code written in many places. I want to refactor the code to be able to use JavaScript compiler for performance and syntax check reasons. What is the recommendation to prepare the code for compiling?
- I have JavaScript code written with EJS to render some server-side data in JavaScript:
Code:
<% if(product.type){ %>
$('#someid').html('<%= product.type %>'); //javascript
<% } %>
- I think the compiler will change my variables name (I know it can be set not to change which variable, but I have so many of them used across the pages, scripts, or widgets/subpages; something like global
var
), in that case it'll surely collapse my code.
I've heard that writing in closure style helps variable collision between each script files. Do I have to rewrite all code using that pattern? Do I have to construct the code from scatch? I'm asking for the way to prepare the code to be compiled, any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器不会更改全局名称和属性名称。它只是重命名局部变量并删除空格。
The compiler does not change global and property names. It just renames the local variables and removes the whitespace.