Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何同时使用下划线和下划线.string?_Javascript_Underscore.js - Fatal编程技术网

Javascript 如何同时使用下划线和下划线.string?

Javascript 如何同时使用下划线和下划线.string?,javascript,underscore.js,Javascript,Underscore.js,我在项目中使用下划线,但现在我想使用 我阅读了他们的文档,如果我不采取他们说要采取的额外措施,我可能会在使用这两种方法时遇到问题: var _ = require('underscore'); // Import Underscore.string to separate object, because there are conflict functions (include, reverse, contains) _.str = require('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
但是,我不知道如何遵循这些步骤,我需要帮助理解这两个步骤,并且在使用它们时没有问题

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

<script src="underscore-min.js" type="text/javascript"></script>
<script src="underscore.string.min.js" type="text/javascript"></script>

另外一个相关的问题是,当我设法同时使用这两个函数时,我必须始终使用
.str
,还是仅在冲突函数上使用?

如何使用下划线.string显然是为在node.js中使用而编写的,但是当您想在html/js中使用它时,您已经从包含这两个库开始了

下划线将创建
变量,如果存在该变量,下划线.string将通过
str
string
属性扩展该变量,因此您可以通过包含这两个文件来使用
.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>

_.mixin(u.str.exports())
如果将最后一行添加到include中,则可以使用任何非冲突的方法,从下划线.string中可以看出,它几乎是
include
包含
反向


希望对您有所帮助。

如果您希望对每个函数(即使是冲突的函数)使用
\u
而不是
.str
,您可以根据参数类型动态选择冲突的方法:

(function(_contains, _include) {
    _.mixin(_.str.exports());
    _.mixin({
        reverse: function(obj) {
            if (typeof obj === "string") {
                return _.str.reverse(obj);
            }
            return obj.reverse();
        },
        contains: function(obj, value) {
            if (typeof obj === "string") {
                return _.str.contains(obj, value);
            }
            return _contains.apply(this, arguments);
        },
        include: function(obj, value) {
            if (typeof obj === "string") {
                return _.str.include(obj, value);
            }
            return _include.apply(this, arguments);
        }
    });
})(_.contains, _.include);

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

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

<script src="underscore-min.js" type="text/javascript"></script>
<script src="underscore.string.min.js" type="text/javascript"></script>

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


对于下划线.string版本3.x来说,这已经过时,新版本只是导出一个全局
s
,而不是混入下划线命名空间。关于v3.x,请参见下面的答案