问题
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:
- Tell JSLint not to bother even looking at them two lines.
- Suppress the
Weird assignment
error. - 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