zoukankan      html  css  js  c++  java
  • underscore源码解析(实用的功能)

    // Run Underscore.js in *noConflict* mode, returning the `_` variable to its
    // previous owner. Returns a reference to the Underscore object.
    //让渡_变量的控制权,以防冲突
    _.noConflict = function() {
    root._ = previousUnderscore;
    return this;
    };
    // Keep the identity function around for default iteratees.
    //默认的迭代器,直接返回参数
    _.identity = function(value) {
    return value;
    };

    // Predicate-generating functions. Often useful outside of Underscore.
    //不知道在哪里用的。。。。
    _.constant = function(value) {
    return function() {
    return value;
    };
    };

    _.noop = function(){};
    //返回键的值
    _.property = function(key) {
    return function(obj) {
    return obj == null ? void 0 : obj[key];
    };
    };

    // Generates a function for a given object that returns a given property.
    //和上面那个反一下
    _.propertyOf = function(obj) {
    return obj == null ? function(){} : function(key) {
    return obj[key];
    };
    };

    // Returns a predicate for checking whether an object has a given set of
    // `key:value` pairs.
    //obj对象和json的匹配
    _.matcher = _.matches = function(attrs) {
    attrs = _.extendOwn({}, attrs);
    return function(obj) {
    return _.isMatch(obj, attrs);
    };
    };

    // Run a function **n** times.
    //执行N次函数
    _.times = function(n, iteratee, context) {
    var accum = Array(Math.max(0, n));
    iteratee = optimizeCb(iteratee, context, 1);
    for (var i = 0; i < n; i++) accum[i] = iteratee(i);
    return accum;
    };

    // Return a random integer between min and max (inclusive).
    //随机数
    _.random = function(min, max) {
    if (max == null) {
    max = min;
    min = 0;
    }
    return min + Math.floor(Math.random() * (max - min + 1));
    };

    // A (possibly faster) way to get the current timestamp as an integer.
    //当前时间撮
    _.now = Date.now || function() {
    return new Date().getTime();
    };

    // List of HTML entities for escaping.
    var escapeMap = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#x27;',
    '`': '&#x60;'
    };
    //对象的键值互换位置
    var unescapeMap = _.invert(escapeMap);

    // Functions for escaping and unescaping strings to/from HTML interpolation.
    //html转换器
    var createEscaper = function(map) {
    var escaper = function(match) {
    return map[match];
    };
    // Regexes for identifying a key that needs to be escaped
    var source = '(?:' + _.keys(map).join('|') + ')';
    var testRegexp = RegExp(source);
    var replaceRegexp = RegExp(source, 'g');
    return function(string) {
    string = string == null ? '' : '' + string;
    return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
    };
    };



    // If the value of the named `property` is a function then invoke it with the
    // `object` as context; otherwise, return it.

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

    // Generate a unique integer id (unique within the entire client session).
    // Useful for temporary DOM ids.
    //唯一的临时ID
    var idCounter = 0;
    _.uniqueId = function(prefix) {
    var id = ++idCounter + '';
    return prefix ? prefix + id : id;
    };

    哈哈哈哈哈,写的差不多了,其实underscore已经是早就过时的东西了,不过看看也有帮助(主要是其他太难,看不懂,哈哈哈哈哈)
  • 相关阅读:
    日志
    mysql锁
    慢查询
    auto_increment
    脚本
    服务器元数据
    复制表及表数据
    临时表
    (一)校园信息通微信小程序从前端到后台整和笔记
    OpenCart框架运行流程介绍opencart资料链接
  • 原文地址:https://www.cnblogs.com/wz0107/p/4982951.html
Copyright © 2011-2022 走看看