一、_.noConflict:命名冲突处理方法javascript
_.noConflict = function() { root._ = previousUnderscore;
//返回this不错 return this; };
二、_.identity():默认的迭代处理器html
_.identity = function(value) { return value; };
三、_.times():调用指定的迭代器n次java
_.times = function (n, iterator, context) { for (var i = 0; i < n; i++) iterator.call(context, i); };
四、_.escape():转义html代码;_.unescape()与前者相反api
_.escape = function(string) {
//string转为字符串:''+string return (''+string).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/'); };
五、_result():返回对象指定属性的值,若是为函数,则返回执行的值app
_.result = function(object, property) { if (object == null) return null; var value = object[property]; return _.isFunction(value) ? value.call(object) : value; };
六、_.mixin():用本身的程序扩展underscore, ide
_.mixin = function(obj) { each(_.functions(obj), function(name){
//将自定义方法添加到underscore对象中,支持对象式调用;同时加入到_中,支持函数式调用 addToWrapper(name, _[name] = obj[name]); }); };
_.mixin({ capitalize: function(string) { return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); } }); _("fabio").capitalize(); => "Fabio"
七、_.uniqueId():给对象或DOM建立惟一ID函数
_.uniqueId = function(prefix) { var id = idCounter++; return prefix ? prefix + id : id; };
八、_.templateSettings():定义模板的界定符号this
_.templateSettings = { evaluate : /<%([\s\S]+?)%>/g, interpolate : /<%=([\s\S]+?)%>/g, escape : /<%-([\s\S]+?)%>/g };
九、_.template():比较麻烦lua