JSLint Weird赋值,闭包编译器必需 [英] JSLint Weird assignment, required for closure compiler

查看:110
本文介绍了JSLint Weird赋值,闭包编译器必需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用闭包编译器来简化和加速代码,但是当我尝试

I'm using the closure compiler to minify and speed up my code but I'm running into some issues with JSLint when I try to export my functions.

基本上,我有一个带有功能foo.bar()的对象foo{},该对象通过外部文件as调用.为了从外部调用此函数,我需要在脚本进行编译之前为其添加一些声明:

Basically, I have an object, foo{} with a function, foo.bar() that gets called via an external file as. In order for this function to be called externally I need to add some declarations to my script before it gets compiled:

window['foo'] = foo;
window['foo']['bar'] = foo.bar;

这很好用,但是-与以往一样-JSLint认为我即使尝试也很有心智.我设法通过在这两行之前声明/*jslint sub: true */来抑制点表示法错误,但仍然出现以下错误:

This works great, but—as ever—JSLint thinks I'm mental for even attempting this. I've managed to suppress the dot notation error by declaring, /*jslint sub: true */ just before these two lines but I still get the following error:

"window['foo']['bar'] = foo.bar;" - Weird assignment

这没错,这是上下文之外的怪异任务,但是我需要在那里才能使我的代码正常工作.

It's not wrong, it is a weird assignment out of context, but I need it in there in order for my code to work.

从我的角度来看,我有三个可能的选择:

The way I see it, I have three possible options:

  1. 告诉JSLint甚至不要看着他们两行.
  2. 抑制Weird assignment错误.
  3. 找到使我的代码与闭包编译器一起工作的另一种方法.
  1. Tell JSLint not to bother even looking at them two lines.
  2. Suppress the Weird assignment error.
  3. Find another way to make my code work with closure compiler.

问题是,我不知道该怎么做.

The problem is, I have no idea how to go about doing any of them.

推荐答案

您可以使用goog.exportSymbol而不是括号符号导出名称:

You can export names using goog.exportSymbol instead of bracket notation: https://github.com/google/closure-library/blob/master/closure/goog/base.js#L1532

Closure编译器了解goog.exportSymbol是什么,因此它将删除显式的exportSymbol调用,并将foobar直接添加到窗口中.

The Closure Compiler understands what goog.exportSymbol is so it'll remove the explicit exportSymbol call and add foo and bar directly to the window for you.

这篇关于JSLint Weird赋值,闭包编译器必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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