如何同时使用下划线和下划线?

aja*_*221 13 javascript underscore.js

我在我的项目中使用下划线,但现在我想使用underscore.string扩展它

我阅读了他们的文档,如果我不采取他们所说的额外措施,我似乎可能会遇到问题:

var _  = require('underscore');

// Import Underscore.string to separate object, because there are conflict functions (include, reverse, contains)
_.str = require('underscore.string');

// Mix in non-conflict functions to Underscore namespace if you want
_.mixin(_.str.exports());

// All functions, include conflict, will be available through _.str object
_.str.include('Underscore.string', 'string'); // => true
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何遵循这些步骤,我需要帮助了解使用它们的步骤,并且使用它们没有任何问题.

到目前为止,我已经这样做了:

<script src="underscore-min.js" type="text/javascript"></script>
<script src="underscore.string.min.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

另外一个相关的问题,当我设法与两者合作时,我必须始终使用_.str,还是只使用冲突的功能?

Tha*_*bas 20

操作方法使用underscore.string显然是写在node.js的用法,但只要你想在HTML/JS使用它,你已经向右包括两个库开始.

下划线将创建_var,如果存在,则underscore.string将通过strstring属性扩展它,因此您可以_.str通过包含这两个文件来使用它.

<script src="underscore-min.js" type="text/javascript"></script>
<script src="underscore.string.min.js" type="text/javascript"></script>
<script type="text/javascript"> _.mixin(_.str.exports()) </script>
Run Code Online (Sandbox Code Playgroud)

如果您在最后一行添加到您的包含,你可以使用任何非冲突的方法underscore.string这是所有,但include,containsreverse根据源文件.

希望有所帮助.

  • 这对于underscore.string版本3.x已经过时了,新版本只是将全局`s`而不是mixin导出到下划线命名空间.请参阅下面的v3.x答案 (2认同)

ste*_*red 5

在 3.x 版中,只需使用全局s而不是_.mixin()下划线命名空间。

以下是 Underscore.string 的变更日志说明

这是 3.x 版所需的全部内容

<script src="underscore-min.js" type="text/javascript"></script>
<script src="underscore.string.min.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

现在不鼓励使用 Underscore.js 或 Lo-Dash mixin,因为有太多的碰撞方法