dongxixian7803 2012-02-13 18:26
浏览 122
已采纳

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

I am using underscore on my projects, but now I want to extend it a using the underscore.string

I read their documentation, and it seems I could have problems using both if I don't take the extra measures they say to take:

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

However, I don't have any idea of how to follow those steps, I need help understanding the steps to work with both and have no trouble using them.

So far, I have done this:

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

Also another related question, when I manage to work with both, must I always use _.str, or only on the conflicted functions?

  • 写回答

3条回答 默认 最新

  • douzhanglun4482 2012-02-13 19:12
    关注

    The How-To use underscore.string is apparently written for usage in node.js, but as you want to use it within html/js you already started right by including those both libraries.

    underscore will create the _ var and if that is present, underscore.string will extend that by a str and string property so you may use _.str already by including the two files.

    <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>
    

    If you add that last line to your includes, you may use any non conflicting method from underscore.string which is all but include, contains and reverse according to the source file.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教