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