Fill in missing properties in object with default values from the defaults objects, and return the object. As soon as the property is filled, further defaults will have no effect.

将默认属性和值填充给对象。再次填充将没有效果

1 var iceCream = {flavor : "chocolate"};
2 _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"});
3 => {flavor : "chocolate", sprinkles : "lots"}

源码:

1   _.defaults = function(obj) {
2     each(slice.call(arguments, 1), function(source) {
3       for (var prop in source) {
4         if (obj[prop] == null) obj[prop] = source[prop];
5       }
6     });
7     return obj;
8   };

 

 

 

posted on 2012-04-16 21:58  himanhimao  阅读(468)  评论(0编辑  收藏  举报