问题
I'll try to summarize a few steps and ask a question after
underscore.jsfailed 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.jscode, it was found that ifexportsormodule.exportsis defined, than the_variable will become a member ofexportsormodule.exports. It just happened (in browser, not in Node.js of course).I've tried to save the
exportsobject aside and delete it before loadingunderscore.jsand restore it right after callingnoConflict(). However, when callingdelete exports, I've gotfalse, which means thatexportscan't be deleted.I've found two member functions in
exportsobject:compile()andcompileToString(). By looking into their implementation I've foundLFreferences which perhaps refer tolivefyreJavascript 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
exportsobject.Let underscore.js to modify the
exportsobject 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