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():比较麻烦

  • 相关阅读:
    浅谈 IBM 购并 Sun Microsystems
    用 CSS 替代 HTML 的 table tag 设计网页版面
    用 IIS 7、ARR 與 Velocity 建设高性能的大型网站
    实作 ASP.NET 多笔数据离线编辑
    快速搞懂 ASP.NET MVC
    C# Design Patterns (2) Strategy
    网站性能越来越差怎么办?
    dotNET 类型转型的三种做法
    ASP.NET 数据分页第二篇 范例下载
    程序员真情忏悔录
  • 原文地址:https://www.cnblogs.com/wang-jing/p/4737612.html
Copyright © 2011-2022 走看看