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已是早就過期的東西了,不過看看也有幫助(主要是其餘太難,看不懂,哈哈哈哈哈)
相關文章
相關標籤/搜索