问题
I'll try to summarize a few steps and ask a question after
underscore.js
failed to install when developing a 3rd-party javascript component (as described here: Underscore.js and noconflict). my code included the following line to avoid collisions of different versions of underscore.js:window.$MyUS = _.noConflict();
when looking into the
underscore.js
code, it was found that ifexports
ormodule.exports
is defined, than the_
variable will become a member ofexports
ormodule.exports
. It just happened (in browser, not in Node.js of course).I've tried to save the
exports
object aside and delete it before loadingunderscore.js
and restore it right after callingnoConflict()
. However, when callingdelete exports
, I've gotfalse
, which means thatexports
can't be deleted.I've found two member functions in
exports
object:compile()
andcompileToString()
. By looking into their implementation I've foundLF
references which perhaps refer tolivefyre
Javascript API (http://www.livefyre.com/docs/javascript-sdk-reference)
Which is the better way to resolve the conflict? I see the following options:
Modify the underscore.js code so that it won't try to install itself into
exports
object.Let underscore.js to modify the
exports
object and hope that nothing wrong will happen on any site. In that case I will need to re-write thewindow.$MyUS = _.noConflict();
line like this:window.$MyUS = .noConflict || exports..noConflict() || ... ; // sanity checks omitted for clarity, obviously the production code must be more robust
If you need any more info - please ask.
回答1:
The answer, I think, is simple: fork underscore on GitHub and add a simple patch that refuses to attach itself to exports
if window
exists, and submit a pull request.
That way everyone benefits: you don't have re-apply your patch every time you upgrade your copy of underscore and others don't have to deal with that problem, either.
来源:https://stackoverflow.com/questions/11615610/underscore-js-global-object-named-exports-and-livefyre-javascript-api-integ