JSLint Weird assignment, required for closure compiler

回眸只為那壹抹淺笑 提交于 2019-12-11 05:29:14

问题


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.

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;

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. 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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/25002331/jslint-weird-assignment-required-for-closure-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!