underscore utility

一、_.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

相關文章
相關標籤/搜索