underscore.js, global object named “exports” and livefyre javascript API - integration conflict

余生颓废 提交于 2020-01-15 03:48:06

问题


I'll try to summarize a few steps and ask a question after

  1. 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();

  2. when looking into the underscore.js code, it was found that if exports or module.exports is defined, than the _ variable will become a member of exports or module.exports. It just happened (in browser, not in Node.js of course).

  3. I've tried to save the exports object aside and delete it before loading underscore.js and restore it right after calling noConflict(). However, when calling delete exports, I've got false, which means that exports can't be deleted.

  4. I've found two member functions in exports object: compile() and compileToString(). By looking into their implementation I've found LF references which perhaps refer to livefyre Javascript API (http://www.livefyre.com/docs/javascript-sdk-reference)

Which is the better way to resolve the conflict? I see the following options:

  1. Modify the underscore.js code so that it won't try to install itself into exports object.

  2. 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 the window.$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

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