1、_.noConflict:命名冲突处理方法

_.noConflict = function() {
root._ = previousUnderscore;
   //返回this不错
return this;
};

2、_.identity():默认的迭代处理器

_.identity = function(value) {
return value;
};

3、_.times():调用指定的迭代器n次

_.times = function (n, iterator, context) {
for (var i = 0; i < n; i++) iterator.call(context, i);
};

4、_.escape():转义html代码;_.unescape()与前者相反

_.escape = function(string) {
   //string转为字符串:''+string
return (''+string).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
};

5、_result():返回对象指定属性的值,如果为函数,则返回执行的值

_.result = function(object, property) {
if (object == null) return null;
var value = object[property];
return _.isFunction(value) ? value.call(object) : value;
};

6、_.mixin():用自己的程序扩展underscore, 

_.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"

7、_.uniqueId():给对象或DOM创建唯一ID

_.uniqueId = function(prefix) {
var id = idCounter++;
return prefix ? prefix + id : id;
};

8、_.templateSettings():定义模板的界定符号

_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};

9、_.template():比较麻烦

05-28 21:56