javascript - LoDashUnderscorejs not global variable - Stack Overflow

Basically I want to use Lo dash or Underscore (preferably lo-dash) however, with my system it is import

Basically I want to use Lo dash or Underscore (preferably lo-dash) however, with my system it is important that I don't load the variables globally and use them anonymously. If I can't use anon, then at least keeping the current version on the page and not overriding it.

Preferably it would be pletely anonymously, any ideas on how to go about this would be greatly appreciated

It's not possible to use require either

Basically I want to use Lo dash or Underscore (preferably lo-dash) however, with my system it is important that I don't load the variables globally and use them anonymously. If I can't use anon, then at least keeping the current version on the page and not overriding it.

Preferably it would be pletely anonymously, any ideas on how to go about this would be greatly appreciated

It's not possible to use require either

Share Improve this question asked Jun 19, 2014 at 18:19 Nick WhiteNick White 1,6124 gold badges20 silver badges36 bronze badges 6
  • 1 lodash./docs#noConflict ? – Evan Davis Commented Jun 19, 2014 at 18:24
  • Ah I hadn't realised, thanks. For example if _ is undefined before running noConflict I guess it would remove it? – Nick White Commented Jun 19, 2014 at 18:33
  • Although if possible to be pletely "anon" would be better I think – Nick White Commented Jun 19, 2014 at 18:41
  • What does that even mean? You can do yournamespace._ = _.noConflict()...? – Evan Davis Commented Jun 19, 2014 at 18:41
  • It never gets loaded on to the window, basically if I did something like (function (){ var x = 'something'; })(); I wouldn't have access to x outside of that .... – Nick White Commented Jun 19, 2014 at 18:55
 |  Show 1 more ment

2 Answers 2

Reset to default 8

This is a use case for _.noConflict().

(function() {
    var x = _.noConflict();

    // logs undefined
    console.info('lodash', window._);

}());

Demo

Using lodash-cli https://github./lodash/lodash-cli, it is possible to do this pletely making a custom build wrapping the output of Lo-dash using the IIFE option.

Normal is something like iife="!function(window,undefined){%output%}(this)"

Using lodash-cli it is also possible to only build functions from lodash which is desired.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745399130a4626019.html

相关推荐

  • javascript - LoDashUnderscorejs not global variable - Stack Overflow

    Basically I want to use Lo dash or Underscore (preferably lo-dash) however, with my system it is import

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信