首页 > underscore.string下载正确引用后为什么会报错,非node方式

underscore.string下载正确引用后为什么会报错,非node方式

<meta charset="utf-8"/>
<script src="../underscore.js"></script>
<script src="../underscore.string.js"></script>
<script src="../jquery.js"></script>
<script src="../backbone.js"></script>
<script>
//TypeError: _(...).chain(...).trim is not a function
    console.log(_("   epeli  ").chain().trim().capitalize().value());
</script>

是因为_.string这个扩展没有融入到_对象里面去,所以你新建的_对象没有包括string扩展的函数导致的,再运行之前加上这句把扩展导入进去就好了。

_.mixin(_.string.exports())


var test = "   this is a test    ";
console.log(test);    // "   this is a test    "
test = _.str.trim(test);
console.log(test);    // "this is a test"
test = _.str.capitalize(test);
console.log(test);    // "This is a test"

最好看下源码,现在最新的underscore.string 3.1.1暴露出来的对象是s。感觉underscore.string要独立于underscore似的

【热门文章】
【热门文章】