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.//惟一的临时IDvar idCounter = 0;_.uniqueId = function(prefix) {    var id = ++idCounter + '';    return prefix ? prefix + id : id;};哈哈哈哈哈,写的差很少了,其实underscore已是早就过期的东西了,不过看看也有帮助(主要是其余太难,看不懂,哈哈哈哈哈)
相关文章
相关标签/搜索