使用Google Closure编译器部分跳过部分 [英] Partially skip sections with Google Closure Compiler

查看:119
本文介绍了使用Google Closure编译器部分跳过部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在服务器上生成JavaScript,并且希望运行Google Clousure编译器以在脚本的php源代码上运行.

I'm generating a javascript on the server like and would like to run Google Clousure Compiler to be ran on the php source code of the script.

var jsvar = <?=$var ? true : false ?>;

只是在徘徊是否有任何方法告诉编译器跳过对?的优化?就像正则表达式跳过一样:

Just wandering if there is any way in telling the compiler to skip optimazation of ? Like a regexp skip:

/<\?=.*?\?>/

最诚挚的问候,

尼古拉斯

推荐答案

我发现,当我将客户端JavaScript与服务器端逻辑分离时,我的代码更易于维护.现在,我编写脚本,以便服务器端处理发出初始化变量.

I have found that my code is much easier to maintain when I separate my client-side JavaScript from my server-side logic. Now I write my scripts such that my server-side processing emits initialization variables.

示例-服务器端:

<?php echo 'var mynamespace = {}; mynamespace.jsvar = "' . $var . '";'; ?>

在我的客户端javascript中:

And in my client-side javascript:

var mynamespace = window['mynamespace'] || {};
function MyFunction() {
  alert(mynamespace['jsvar']);
}
MyFunction();

使用这种样式,我的客户端javascript可以使用Closure-compiler轻松编译.

Using this style, my client-side javascript compiles easily with Closure-compiler.

这篇关于使用Google Closure编译器部分跳过部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆