码迷,mamicode.com
首页 > 其他好文 > 详细

backbone和underscore中的extend

时间:2016-06-05 11:04:27      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

总是把这两个库中的extend搞混了所以写下来。

backbone中的extend实现了继承:

技术分享
 1   // Helper function to correctly set up the prototype chain for subclasses.
 2   // Similar to `goog.inherits`, but uses a hash of prototype properties and
 3   // class properties to be extended.
 4   var extend = function(protoProps, staticProps) {
 5     var parent = this;
 6     var child;
 7 
 8     // The constructor function for the new subclass is either defined by you
 9     // (the "constructor" property in your `extend` definition), or defaulted
10     // by us to simply call the parent constructor.
11     if (protoProps && _.has(protoProps, ‘constructor‘)) {
12       child = protoProps.constructor;
13     } else {
14       child = function(){ return parent.apply(this, arguments); };
15     }
16 
17     // Add static properties to the constructor function, if supplied.
18     _.extend(child, parent, staticProps);
19 
20     // Set the prototype chain to inherit from `parent`, without calling
21     // `parent`‘s constructor function and add the prototype properties.
22     child.prototype = _.create(parent.prototype, protoProps);
23     child.prototype.constructor = child;
24 
25     // Set a convenience property in case the parent‘s prototype is needed
26     // later.
27     child.__super__ = parent.prototype;
28 
29     return child;
30   };
31 
32   // Set up inheritance for the model, collection, router, view and history.
33   Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend;
View Code

underscore中的extend实现了浅复制:

技术分享
 1 _.extend = createAssigner(_.allKeys);
 2 
 3 var createAssigner = function(keysFunc, undefinedOnly) {
 4     return function(obj) {
 5       var length = arguments.length;
 6       if (length < 2 || obj == null) return obj;
 7       for (var index = 1; index < length; index++) {
 8         var source = arguments[index],
 9             keys = keysFunc(source),
10             l = keys.length;
11         for (var i = 0; i < l; i++) {
12           var key = keys[i];
13           if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
14         }
15       }
16       return obj;
17     };
18   };
View Code

 

backbone和underscore中的extend

标签:

原文地址:http://www.cnblogs.com/wangwei1314/p/5560148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!