underscore源码解析(object)
2015-11-19 13:39
525 查看
//用来变对象的。可以没有value直接传list,把list变一个对象 //也可以传长度和list一样的value,来构成数组 _.object = function(list, values) { var result = {}; for (var i = 0, length = list && list.length; i < length; i++) { if (values) { result[list[i]] = values[i]; } else { result[list[i][0]] = list[i][1]; } } return result; }; //获取对象的所有键集合,有原生的nativeKeys,就用原生。 //貌似ie9以下有个枚举bug,实例的toString会覆盖原型上的toString,toString就变成不可枚举了,都没试出来过。。。。 //!({toString: null}).propertyIsEnumerable('toString'); _.keys = function(obj) { if (!_.isObject(obj)) return []; if (nativeKeys) return nativeKeys(obj); var keys = []; for (var key in obj) if (_.has(obj, key)) keys.push(key); // Ahem, IE < 9. if (hasEnumBug) collectNonEnumProps(obj, keys); return keys; }; // 所有键列出来,实例和原型的 _.allKeys = function(obj) { if (!_.isObject(obj)) return []; var keys = []; for (var key in obj) keys.push(key); // Ahem, IE < 9. if (hasEnumBug) collectNonEnumProps(obj, keys); return keys; }; // 实例键的值组成一个数组 _.values = function(obj) { var keys = _.keys(obj); var length = keys.length; var values = Array(length); for (var i = 0; i < length; i++) { values[i] = obj[keys[i]]; } return values; }; //类似map,只不过obj是对象,用iteratee迭代每个键值对,返回新数组 _.mapObject = function(obj, iteratee, context) { iteratee = cb(iteratee, context); var keys = _.keys(obj), length = keys.length, results = {}, currentKey; for (var index = 0; index < length; index++) { currentKey = keys[index]; results[currentKey] = iteratee(obj[currentKey], currentKey, obj); } return results; }; //把一个对象转变为一个[key, value]形式的数组。 _.pairs = function(obj) { var keys = _.keys(obj); var length = keys.length; var pairs = Array(length); for (var i = 0; i < length; i++) { pairs[i] = [keys[i], obj[keys[i]]]; } return pairs; }; // 对象键值对互换位置,要保证键值对中值的唯一性 _.invert = function(obj) { var result = {}; var keys = _.keys(obj); for (var i = 0, length = keys.length; i < length; i++) { result[obj[keys[i]]] = keys[i]; } return result; }; //把对象中的函数堆到一个数组中,函数名按字母a-z排序 _.functions = _.methods = function(obj) { var names = []; for (var key in obj) { if (_.isFunction(obj[key])) names.push(key); } return names.sort(); }; //对象的扩展,扩展传入的所有属性,包括原型链 _.extend = createAssigner(_.allKeys); ////对象的扩展,扩展传入的所有属性,不包括原型链 _.extendOwn = _.assign = createAssigner(_.keys);
// Return a copy of the object only containing the whitelisted properties. //返回obj,包含符合条件的键值对 _.pick = function(object, oiteratee, context) { var result = {}, obj = object, iteratee, keys; if (obj == null) return result; if (_.isFunction(oiteratee)) { keys = _.allKeys(obj); iteratee = optimizeCb(oiteratee, context); } else { keys = flatten(arguments, false, false, 1); iteratee = function(value, key, obj) { return key in obj; }; obj = Object(obj); } for (var i = 0, length = keys.length; i < length; i++) { var key = keys[i]; var value = obj[key]; if (iteratee(value, key, obj)) result[key] = value; } return result; }; // Return a copy of the object without the blacklisted properties. //返回obj,过滤掉符合条件的键值对 _.omit = function(obj, iteratee, context) { if (_.isFunction(iteratee)) { iteratee = _.negate(iteratee); } else { var keys = _.map(flatten(arguments, false, false, 1), String); iteratee = function(value, key) { return !_.contains(keys, key); }; } return _.pick(obj, iteratee, context); }; // Fill in a given object with default properties. //对象扩展,对象的键是undefined的话就填充,否则就保留原来的 _.defaults = createAssigner(_.allKeys, true); // Creates an object that inherits from the given prototype object. // If additional properties are provided then they will be added to the // created object. _.create = function(prototype, props) { var result = baseCreate(prototype); if (props) _.extendOwn(result, props); return result; }; // Create a (shallow-cloned) duplicate of an object. //克隆对象或数组 _.clone = function(obj) { if (!_.isObject(obj)) return obj; return _.isArray(obj) ? obj.slice() : _.extend({}, obj); }; // Invokes interceptor with the obj, and then returns obj. // The primary purpose of this method is to "tap into" a method chain, in // order to perform operations on intermediate results within the chain. //调用interceptor,再传惨obj。返回obj _.tap = function(obj, interceptor) { interceptor(obj); return obj; }; // Returns whether an object has a given set of `key:value` pairs. //传的键值对否在obj当中 _.isMatch = function(object, attrs) { var keys = _.keys(attrs), length = keys.length; if (object == null) return !length; var obj = Object(object); for (var i = 0; i < length; i++) { var key = keys[i]; if (attrs[key] !== obj[key] || !(key in obj)) return false; } return true; };
// Perform a deep comparison to check if two objects are equal. //2个对象是否相等 _.isEqual = function(a, b) { return eq(a, b); }; // Is a given array, string, or object empty? // An "empty" object has no enumerable own-properties. //传的东西是否为空 _.isEmpty = function(obj) { if (obj == null) return true; if (isArrayLike(obj) && (_.isArray(obj) || _.isString(obj) || _.isArguments(obj))) return obj.length === 0; return _.keys(obj).length === 0; }; // Is a given value a DOM element? //是不是DOM元素 _.isElement = function(obj) { return !!(obj && obj.nodeType === 1); }; // Is a given value an array? // Delegates to ECMA5's native Array.isArray //是不是数组 _.isArray = nativeIsArray || function(obj) { return toString.call(obj) === '[object Array]'; }; // Is a given variable an object? //是不是对象 _.isObject = function(obj) { var type = typeof obj; return type === 'function' || type === 'object' && !!obj; }; // Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError. //对每个原生类调用tostring方法,判断是否是原生对象 _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) { _['is' + name] = function(obj) { return toString.call(obj) === '[object ' + name + ']'; }; }); // Define a fallback version of the method in browsers (ahem, IE < 9), where // there isn't any inspectable "Arguments" type. //参数对象有个callee属性,指向函数本身,用来判断是不是arguments if (!_.isArguments(arguments)) { _.isArguments = function(obj) { return _.has(obj, 'callee'); }; } // Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8, // IE 11 (#1621), and in Safari 8 (#1929). // 另一种方法用Object.prototype.toString.call(obj)==='[object Function]'; if (typeof /./ != 'function' && typeof Int8Array != 'object') { _.isFunction = function(obj) { return typeof obj == 'function' || false; }; } // Is a given object a finite number? _.isFinite = function(obj) { return isFinite(obj) && !isNaN(parseFloat(obj)); }; // Is the given value `NaN`? (NaN is the only number which does not equal itself). _.isNaN = function(obj) { return _.isNumber(obj) && obj !== +obj; }; // Is a given value a boolean? _.isBoolean = function(obj) { return obj === true || obj === false || toString.call(obj) === '[object Boolean]'; }; // Is a given value equal to null? _.isNull = function(obj) { return obj === null; }; // Is a given variable undefined? _.isUndefined = function(obj) { return obj === void 0; }; // Shortcut function for checking if an object has a given property directly // on itself (in other words, not on a prototype). //http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/防止意外覆盖原型上的方法 _.has = function(obj, key) { return obj != null && hasOwnProperty.call(obj, key); };
相关文章推荐
- Bucket不为空,请检查该Bucket是否包含未删除的Object或者未成功的Multipart碎片
- 初学Objective-C
- Objective-C 编码建议
- Objective-C基础-类方法的学习
- iOS开发系列—Objective-C之Foundation框架
- Objective-C 编码建议
- Objective-C 编码建议
- Java Object 类源码分析
- Objective-c:类目、延展、协议
- object references an unsaved transient instance
- _nm_object_get_property: Error getting 'State' for /org/freedesktop/NetworkManager/ActiveConnection/
- Java不定参数Object… obj
- [2015.11.18] Objective-c UIScrollView 页面跳转 线传值
- Objective-C 学习笔记 - 第1章 入门
- Objective-C 学习笔记 - 第1章 入门
- Object-C排序的几种方法
- Geometry-Shader Object
- 新书Effective Objective-C 2.0
- Object-C中的集合类
- Objective-C 编码建议