最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - LoDashUnderscorejs not global variable - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论