zoukankan      html  css  js  c++  java
  • underscore utility

    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    : /<%([sS]+?)%>/g,
        interpolate : /<%=([sS]+?)%>/g,
        escape      : /<%-([sS]+?)%>/g
      };
    

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

  • 相关阅读:
    PV、UV、VV、IP是什么意思?
    多用户远程linux
    实用性阅读指南读书笔记
    在VS添加万能头文件 操作
    sqrt函数实现之卡马克方法
    大端法、小端法及其判断方法
    STL源码剖析之ROUND_UP函数实现原理及其拓展
    海康和多益面经
    小米实习生面试面经
    阿里c++一面面经
  • 原文地址:https://www.cnblogs.com/wang-jing/p/4737612.html
Copyright © 2011-2022 走看看