随笔分类 -  underscore.js

underscore.js _.isEmpty(Object)
摘要:Returnstrueifobjectcontains no values.用于检测一个对象是否是空的1 _.isEmpty([1, 2, 3]);2 => false3 _.isEmpty({});4 => true源码:1 _.isEmpty = function(obj) {2 if (obj == null) return true;3 if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;4 for (var key in obj) if (_.has(obj, key)) retur... 阅读全文
posted @ 2012-04-16 22:56 himanhimao 阅读(1453) 评论(0) 推荐(0)
underscore.js _.isEqual[Object]
摘要:Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.用于比较两个对象的值是否相当,而不是内存引用位置.1 var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};2 var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};3 moe == clone;4 => false5 _.is 阅读全文
posted @ 2012-04-16 22:52 himanhimao 阅读(620) 评论(0) 推荐(0)
underscore.js _has[Object]
摘要:Does the object contain the given key? Identical toobject.hasOwnProperty(key), but uses a safe reference to thehasOwnPropertyfunction, in case it's been功能等同于object.hasOwnProperty(key),用于检测对象是否存在该属性.1 _.has({a: 1, b: 2, c: 3}, "b");2 => true源码:1 _.has = function(obj, key) {2 return h 阅读全文
posted @ 2012-04-16 22:43 himanhimao 阅读(198) 评论(0) 推荐(0)
underscore.js _tap[Object]
摘要:Invokesinterceptorwith theobject, and then returnsobject. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain对象的拦截器,用于在对象方法链中,调试对象1 _.chain([1,2,3,200])2 .filter(function(num) { return num % 2 == 0; })3 阅读全文
posted @ 2012-04-16 22:33 himanhimao 阅读(209) 评论(0) 推荐(0)
underscore.js _.clone[Object]
摘要:Create a shallow-copied clone of theobject. Any nested objects or arrays will be copied by reference, not duplicated.创建对象的浅拷贝克隆.如果是值类型,则拷贝值;如果是引用类型,则拷贝引用地址。1 _.clone({name : 'moe'});2 => {name : 'moe'};源码:1 _.clone = function(obj) {2 if (!_.isObject(obj)) return obj;3 return _.isA 阅读全文
posted @ 2012-04-16 22:14 himanhimao 阅读(466) 评论(0) 推荐(0)
underscore.js _.defaults[Object]
摘要:Fill in missing properties inobjectwith default values from thedefaultsobjects, and return theobject. As soon as the property is filled, further defaults will have no effect.将默认属性和值填充给对象。再次填充将没有效果1 var iceCream = {flavor : "chocolate"};2 _.defaults(iceCream, {flavor : "vanilla", 阅读全文
posted @ 2012-04-16 21:58 himanhimao 阅读(475) 评论(0) 推荐(0)
underscore.js _.pick[Object]
摘要:Return a copy of theobject, filtered to only have values for the whitelistedkeys(or array of valid keys).返回一个对象的副本,筛选过滤,只有白名单键的值1 _.pick({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age');2 => {name : 'moe', age : 50}源码: _.pick = function(obj) { var r 阅读全文
posted @ 2012-04-16 21:54 himanhimao 阅读(387) 评论(0) 推荐(0)
underscore.js _.extend[Object]
摘要:Copy all of the properties in thesourceobjects over to thedestinationobject, and return thedestinationobject. It's in-order, so the last source will override properties of the same name in previous arguments.将源对象属性复制到目标对象,并返回目标对象。它是有序的,所以最后的来源将覆盖以前参数名称相同的属性。1 _.extend({name : 'moe'}, {ag 阅读全文
posted @ 2012-04-16 21:42 himanhimao 阅读(828) 评论(0) 推荐(0)
underscore.js_.functions[Object]
摘要:Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object.返回对象所包含的方法的列表,按A-Z排序.别名methods_.functions(_);=> ["all", "any", "bind", "bindAll", "clone", "compact", &qu 阅读全文
posted @ 2012-04-16 21:38 himanhimao 阅读(175) 评论(0) 推荐(0)
underscore.js _.values[Object]
摘要:Return all of the values of theobject's properties.返回对象的所有属性的值1 _.values({one : 1, two : 2, three : 3});2 => [1, 2, 3]源码:1 _.values = function(obj) {2 return _.map(obj, _.identity);3 }; 阅读全文
posted @ 2012-04-16 21:31 himanhimao 阅读(232) 评论(0) 推荐(0)
underscore.js _.keys[Object]
摘要:Retrieve all the names of theobject's properties.返回对象的所有的属性名称1 _.keys({one : 1, two : 2, three : 3});2 => ["one", "two", "three"]源码:1 _.keys = nativeKeys || function(obj) {2 if (obj !== Object(obj)) throw new TypeError('Invalid object');3 var keys = [];4 阅读全文
posted @ 2012-04-16 21:26 himanhimao 阅读(256) 评论(0) 推荐(0)
underscore.js _range[Array]
摘要:A function to create flexibly-numbered lists of integers, handy foreachandmaploops.start, if omitted, defaults to0;stepdefaults to1. Returns a list of integers fromstarttostop, incremented (or decremented) bystep, exclusive.建立一个包含指定范围单元的数组 1 _.range(10); 2 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 3 _.ra 阅读全文
posted @ 2012-04-15 23:43 himanhimao 阅读(280) 评论(0) 推荐(0)
underscore.js _.lastIndexOf[Array]
摘要:Returns the index of the last occurrence ofvaluein thearray, or-1if value is not present. Uses the nativelastIndexOffunction if possible返回值在数组中最后出现的位置,如果没有出现,返回-11 _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);2 => 4源码:1 _.lastIndexOf = function(array, item) {2 if (array == null) return -1;3 if (nat... 阅读全文
posted @ 2012-04-15 23:39 himanhimao 阅读(284) 评论(0) 推荐(0)
underscore.js _indexOf[Array]
摘要:Returns the index at whichvaluecan be found in thearray, or-1if value is not present in thearray. Uses the nativeindexOffunction unless it's missing. If you're working with a large array, and you know that the array is already sorted, passtrueforisSortedto use a faster binary search.返回值在数组中首 阅读全文
posted @ 2012-04-15 23:35 himanhimao 阅读(448) 评论(0) 推荐(0)
underscore.js _.zip[Array]
摘要:Merges together the values of each of thearrayswith the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. If you're working with a matrix of nested arrays,zip.applycan transpose the matrix in a similar fashion将相对 阅读全文
posted @ 2012-04-15 23:30 himanhimao 阅读(319) 评论(0) 推荐(0)
underscore.js _uniq[Array]
摘要:Produces a duplicate-free version of thearray, using===to test object equality. If you know in advance that thearrayis sorted, passingtrueforisSortedwill run a much faster algorithm. If you want to compute unique items based on a transformation, pass aniteratorfunctio去除数组中重复的值,返回数组中所有唯一的值。别名unique_. 阅读全文
posted @ 2012-04-15 23:21 himanhimao 阅读(1011) 评论(0) 推荐(0)
underscore.js _.difference[Array]
摘要:Similar towithout, but returns the values fromarraythat are not present in theotherarrays.返回数组中的差集_.difference([1, 2, 3, 4, 5], [5, 2, 10]);=> [1, 3, 4]源码:1 _.difference = function(array) {2 var rest = _.flatten(slice.call(arguments, 1), true);3 return _.filter(array, function(value){ retu... 阅读全文
posted @ 2012-04-15 23:12 himanhimao 阅读(877) 评论(0) 推荐(0)
underscore.js _.intersect[Array]
摘要:Computes the list of values that are the intersection of all thearrays. Each value in the result is present in each of thearrays.返回数组的交集1 _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);2 => [1, 2]源码:1 _.intersection = _.intersect = function(array) {2 var rest = slice.call(arguments, 1);3 ... 阅读全文
posted @ 2012-04-15 23:09 himanhimao 阅读(424) 评论(0) 推荐(0)
underscore.js _.union[Array]
摘要:Computes the union of the passed-inarrays: the list of unique items, in order, that are present in one or more of thearrays.返回数组中的并集_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);=> [1, 2, 3, 101, 10]源码:1 _.union = function() {2 return _.uniq(_.flatten(arguments, true));3 }; 阅读全文
posted @ 2012-04-15 23:07 himanhimao 阅读(322) 评论(0) 推荐(0)
underscore.js _without [Array]
摘要:Returns a copy of thearraywith all instances of thevaluesremoved.===is used for the equality test移除数组中不需要的值,值的数量可以是N_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);=> [2, 3, 4]源码: _.without = function(array) { return _.difference(array, slice.call(arguments, 1)); }; 阅读全文
posted @ 2012-04-15 23:01 himanhimao 阅读(302) 评论(0) 推荐(0)