navigator.useragent引用在闭包编译的js中丢失



我使用了一个类似的简单regex测试

is_safari_or_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit/i.test(navigator.userAgent);

在我的js里。在用闭包编译器(高级优化)编译它之后,它就不再工作了。它仍然给我regex,但缺少navigator.useragent。

注意,我没有使用闭包库,在这一点上也不打算使用。如何保存此测试?

js代码

var browser_type = new RegExp('(iPhone|iPod|iPad).*AppleWebKit', 'i');
function is_safari_or_uiwebview() {
    return browser_type.test(navigator.userAgent);
}
window['is_safari_or_uiwebview'] = is_safari_or_uiwebview;

编译代码

var l=/(iPhone|iPod|iPad).*AppleWebKit/i,l=/(iPhone|iPod|iPad).*AppleWebKit/i;function m(){return l.test(navigator.c)}window.is_safari_or_uiwebview=m;

简单的解决方案:使用括号表示法,如文档中所述。

navigator['userAgent']

但是,当我在中测试你的代码时http://closure-compiler.appspot.com/home,它不会使用高级编译重命名属性名称。

最新更新