【问题标题】:Switching to Lodash from Underscore从下划线切换到 Lodash
【发布时间】:2015-08-30 14:27:02
【问题描述】:

我刚开始支持一个庞大的 Backbone 和 Marionette 应用程序。目前使用Underscore,版本为1.7.0。

我想切换到Lodash 最新版本,以利用我今天从另一篇文章中学到的新功能,_.has() 的深度搜索。我也在使用RequireJS

切换到Lodash 的最安全方法是什么?我是吗

1. bower install lodash
2. update require.confg

    "underscore": "../components/underscore/underscore",
    "underscore.string": "../components/underscore.string/lib/underscore.string",

    and update the values to point to `lodash` paths but keep the property names?

这是一个可能适用于也可能不适用于 Lodash 的示例。这只是一个例子。

convertModels: function convertModels() {
  _.each(this.models, _.bind(function (model) {
    model = this.convertItem(model);
  }, this));
},

这篇文章也是关于“我需要更改一些下划线代码”还是 Lodash 能够处理和产生相同的输出。或者我只需要检查应用程序,看看会发生什么?

【问题讨论】:

  • 很遗憾,因为我的语法不好,我被从这个帖子中扣除了。我尽力了,尤其是英语不是我的母语。虽然变化很小。编辑说的“骨干/木偶”很糟糕。它改成了“Backbone/Marionette”,但我决定把它写成“Backbone and Marionette”,这样更好:(
  • 投反对票的原因可能是因为这不是编程问题。还有很多其他的东西需要修剪。
  • 谢谢。下次我会更小心的。我将编辑帖子并添加代码。

标签: backbone.js underscore.js marionette lodash


【解决方案1】:

前段时间面临同样的任务,并通过 require config 做到了这一点:

require.config({
    paths: {
        // ..
        "lodash": "libs/lodash-3.9.3",
        "backbone": "libs/backbone-1.2.0",
        // ..
    },
    map: {
        "*": {
            // use lodash instead of underscore
            "underscore": "lodash"
        }
    }
}

..这里有详细信息:http://requirejs.org/docs/api.html#config-map 我经常使用 _ 函数,并且在切换后无需更改任何内容。

【讨论】:

  • 我做了类似的东西,而且效果很好!尽管您的配置看起来更酷。我从来没有用过地图。地图有什么用?
  • 好吧,例如,您可以用 lodash 替换下划线,而无需更改主干源..:) 检查我添加的链接以获取详细信息..
猜你喜欢
  • 2011-06-08
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多