[Pkg-javascript-commits] [backbone] 44/74: Cleaned up the inherits function and added some extra comments to make it clear what is happening.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:07 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.3.0
in repository backbone.

commit b04854ef1f0250c27494cf1ea5db063858b04bd9
Author: dxgriffiths <dxgriffiths at gmail.com>
Date:   Wed Nov 3 03:22:25 2010 -0700

    Cleaned up the inherits function and added some extra comments to make it clear what is happening.
---
 backbone.js | 43 +++++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/backbone.js b/backbone.js
index 269bf8a..8561679 100644
--- a/backbone.js
+++ b/backbone.js
@@ -737,20 +737,43 @@
   // Helper function to correctly set up the prototype chain, for subclasses.
   // Similar to `goog.inherits`, but uses a hash of prototype properties and
   // class properties to be extended.
-  var inherits = function(parent, protoProps, classProps) {
-    var child;
-    if (protoProps.hasOwnProperty('constructor')) {
+  var inherits = function(parent, protoProps, childProps) {
+    var child,
+        emptyConstructor = function() {},
+        __super__ = parent.constructor;
+
+    // Create a constructor based on protoProps.constructor or parent.
+    // Has the effect of telescoping back to the main serialization
+    // and initialization functions of Backbone.Model/Collection/View
+    // if no custom constructor is supplied by protoProps.
+    if (protoProps && protoProps.hasOwnProperty('constructor')) {
       child = protoProps.constructor;
+      // Remove the function from protoProps so it isn't mixed in below.
+      delete protoProps.constructor;
     } else {
-      child = function(){ return parent.apply(this, arguments); };
+      child = function() {
+        return parent.apply(this, arguments);
+      };
+    }
+
+    // Create a new level of inheritance without triggering parent().
+    emptyConstructor.prototype = __super__;
+    child.prototype = new emptyConstructor();
+
+    // Extend the child and child prototype if the arguments were supplied.
+    if (protoProps) {
+      _.extend(child.prototype, protoProps);
     }
-    var ctor = function(){};
-    ctor.prototype = parent.prototype;
-    child.prototype = new ctor();
-    _.extend(child.prototype, protoProps);
-    if (classProps) _.extend(child, classProps);
+    if (childProps) {
+      _.extend(child, childProps);
+    }
+
+    // Correctly set child's prototype.constructor back to child, instead of parent.
     child.prototype.constructor = child;
-    child.__super__ = parent.prototype;
+    
+    // Set a convenience property in case the parent's prototype is needed later.
+    child.__super__ = __super__;
+
     return child;
   };
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list