Permalink
Browse files

[major] Use the latest version of the Backbone.extend. Also use lodas…

…h instead of underscore for a minor perf boost.
  • Loading branch information...
1 parent 82eb37a commit 7e1e1485a60ffb0d99c0314aa1e85c9bdabdedf6 @3rd-Eden 3rd-Eden committed Sep 20, 2012
Showing with 14 additions and 30 deletions.
  1. +11 −27 index.js
  2. +3 −3 package.json
View
@@ -1,5 +1,5 @@
'use strict'
-var _ = require('underscore');
+var _ = require('lodash');
/**
* Backbone.js helper functions, these have been copy-pasted from the backbone.js
@@ -8,54 +8,38 @@ var _ = require('underscore');
* @license MIT, http://backbonejs.org
*/
-// 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.
-function inherits(parent, protoProps, staticProps) {
+module.exports = function extend(protoProps, staticProps) {
+ var parent = this;
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 the parent's constructor.
- if (protoProps && protoProps.hasOwnProperty('constructor')) {
+ if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
- child = function child(){ parent.apply(this, arguments); };
+ child = function(){ parent.apply(this, arguments); };
}
- // Inherit class (static) properties from parent.
- _.extend(child, parent);
-
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
- ctor.prototype = parent.prototype;
- child.prototype = new ctor();
+ function Surrogate(){ this.constructor = child; };
+ Surrogate.prototype = parent.prototype;
+ child.prototype = new Surrogate;
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Add static properties to the constructor function, if supplied.
- if (staticProps) _.extend(child, staticProps);
+ _.extend(child, parent, staticProps);
- // Correctly set child's `prototype.constructor`.
- child.prototype.constructor = child;
-
- // Set a convenience property in case the parent's prototype is needed later.
+ // Set a convenience property in case the parent's prototype is needed
+ // later.
child.__super__ = parent.prototype;
return child;
};
-
-// The self-propagating extend function that Backbone classes use.
-module.exports = function extend(protoProps, classProps) {
- var child = inherits(this, protoProps, classProps);
- child.extend = this.extend;
- return child;
-};
-
-// Also expose the inherits method.
-module.exports.inherits = inherits;
View
@@ -1,15 +1,15 @@
{
"name": "extendable",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "Extend constructors",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
- "repository": "",
+ "repository": "git://github.com/3rd-Eden/extendable.git",
"author": "Arnout Kazemier",
"license": "MIT",
"dependencies": {
- "underscore": "1.3.x"
+ "lodash": "0.7.x"
}
}

0 comments on commit 7e1e148

Please sign in to comment.