[Pkg-javascript-commits] [backbone] 45/74: merging dxgriffiths' annotations to 'inherits'.

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 6181445f3e441f41cf15047d68b9bb17ee8ab32d
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Wed Nov 3 12:11:12 2010 -0400

    merging dxgriffiths' annotations to 'inherits'.
---
 backbone.js | 51 +++++++++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 28 deletions(-)

diff --git a/backbone.js b/backbone.js
index 8561679..87560cd 100644
--- a/backbone.js
+++ b/backbone.js
@@ -734,45 +734,40 @@
   // Helpers
   // -------
 
+  // Shared empty constructor function to aid in prototype-chain creation.
+  var ctor = function(){};
+
   // 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, 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')) {
+  var inherits = function(parent, protoProps, staticProps) {
+    var child;
+
+    // The constructor function for the new subclass is either defined by you
+    // (the "constructor" property in your `extend` definition), or defaulted
+    // by us to simply call `super()`.
+    if (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();
+    // Set the prototype chain to inherit from `parent`, without calling
+    // `parent`'s constructor function.
+    ctor.prototype = parent.prototype;
+    child.prototype = new ctor();
 
-    // Extend the child and child prototype if the arguments were supplied.
-    if (protoProps) {
-      _.extend(child.prototype, protoProps);
-    }
-    if (childProps) {
-      _.extend(child, childProps);
-    }
+    // Add prototype properties (instance properties) to the subclass.
+    _.extend(child.prototype, protoProps);
+
+    // Add static properties to the constructor function, if supplied.
+    if (staticProps) _.extend(child, staticProps);
 
-    // Correctly set child's prototype.constructor back to child, instead of parent.
+    // Correctly set child's `prototype.constructor`, for `instanceof`.
     child.prototype.constructor = child;
-    
+
     // Set a convenience property in case the parent's prototype is needed later.
-    child.__super__ = __super__;
+    child.__super__ = parent.prototype;
 
     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