Change straps.js to directly use initialize() as constructor function rather than redirecting calls to it.

Should yield some interesting performance improvements.
This commit is contained in:
Jürg Lehni 2013-05-27 09:11:50 -07:00
parent 094e35f2e5
commit 369b329b23
14 changed files with 24 additions and 21 deletions

View file

@ -252,16 +252,19 @@ var Base = this.Base = new function() { // Straps scope
}, },
extend: function(src/* , ... */) { extend: function(src/* , ... */) {
var ctor = function() { var ctor,
// Call the constructor function, if defined base = this;
if (this.initialize) // Look for an initialize function in all injection scopes and use
return this.initialize.apply(this, arguments); // it as the actual constructor.
for (var i = 0, l = arguments.length; i < l; i++)
if (ctor = arguments[i].initialize)
break;
// If no initialize function is provided, create one that simply
// calls the base constructor.
ctor = ctor || function() {
base.apply(this, arguments);
}; };
ctor.prototype = create(this.prototype); ctor.prototype = create(this.prototype);
// Add a toString function that delegates to initialize if possible
ctor.toString = function() {
return (this.prototype.initialize || function() {}).toString();
};
// The new prototype extends the constructor on which extend is // The new prototype extends the constructor on which extend is
// called. Fix constructor. // called. Fix constructor.
define(ctor.prototype, 'constructor', define(ctor.prototype, 'constructor',

View file

@ -89,7 +89,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{
* }); * });
*/ */
initialize: function(arg) { initialize: function(arg) {
this.base(); Item.call(this);
// Allow Group to have children and named children // Allow Group to have children and named children
this._children = []; this._children = [];
this._namedChildren = {}; this._namedChildren = {};

View file

@ -61,7 +61,7 @@ var Layer = this.Layer = Group.extend(/** @lends Layer# */{
this._project = paper.project; this._project = paper.project;
// Push it onto project.layers and set index: // Push it onto project.layers and set index:
this._index = this._project.layers.push(this) - 1; this._index = this._project.layers.push(this) - 1;
this.base.apply(this, arguments); Group.apply(this, arguments);
this.activate(); this.activate();
}, },

View file

@ -72,7 +72,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
// Support two forms of item initialization: Passing one object literal // Support two forms of item initialization: Passing one object literal
// describing all the different properties to be set, or a symbol (arg0) // describing all the different properties to be set, or a symbol (arg0)
// and a point where it should be placed (arg1). // and a point where it should be placed (arg1).
this.base(arg1 !== undefined && Point.read(arguments, 1)); Item.call(this, arg1 !== undefined && Point.read(arguments, 1));
// If we can handle setting properties through object literal, we're all // If we can handle setting properties through object literal, we're all
// set. Otherwise we need to set symbol. // set. Otherwise we need to set symbol.
if (arg0 && !this._set(arg0)) if (arg0 && !this._set(arg0))

View file

@ -76,7 +76,7 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{
// Support two forms of item initialization: Passing one object literal // Support two forms of item initialization: Passing one object literal
// describing all the different properties to be set, or an image // describing all the different properties to be set, or an image
// (object) and a point where it should be placed (point). // (object) and a point where it should be placed (point).
this.base(position !== undefined && Point.read(arguments, 1)); Item.call(this, position !== undefined && Point.read(arguments, 1));
// If we can handle setting properties through object literal, we're all // If we can handle setting properties through object literal, we're all
// set. Otherwise we need to check the type of object: // set. Otherwise we need to check the type of object:
if (object && !this._set(object)) { if (object && !this._set(object)) {

View file

@ -22,7 +22,7 @@ var Shape = this.Shape = Item.extend(/** @lends Shape# */{
_applyMatrix: false, _applyMatrix: false,
initialize: function(type, point, size) { initialize: function(type, point, size) {
this.base(point); Item.call(this, point);
this._type = type; this._type = type;
this._size = size; this._size = size;
}, },

View file

@ -50,7 +50,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath#
* compoundPath.children[1].position.x += 5; * compoundPath.children[1].position.x += 5;
*/ */
initialize: function(arg) { initialize: function(arg) {
this.base(); PathItem.call(this);
// CompoundPath has children and supports named children. // CompoundPath has children and supports named children.
this._children = []; this._children = [];
this._namedChildren = {}; this._namedChildren = {};

View file

@ -70,7 +70,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
initialize: function(arg) { initialize: function(arg) {
this._closed = false; this._closed = false;
this._segments = []; this._segments = [];
this.base(); Item.call(this);
// arg can either be an object literal describing properties to be set // arg can either be an object literal describing properties to be set
// on the path, a list of segments to be set, or the first of multiple // on the path, a list of segments to be set, or the first of multiple
// arguments describing separate segments. // arguments describing separate segments.

View file

@ -47,7 +47,7 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
initialize: function(view) { initialize: function(view) {
// Activate straight away by passing true to base(), so paper.project is // Activate straight away by passing true to base(), so paper.project is
// set, as required by Layer and DoumentView constructors. // set, as required by Layer and DoumentView constructors.
this.base(true); PaperScopeItem.call(this, true);
this.layers = []; this.layers = [];
this.symbols = []; this.symbols = [];
this._currentStyle = new Style(); this._currentStyle = new Style();

View file

@ -38,7 +38,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
// might be a properties object literal for #setPropeties() at the end. // might be a properties object literal for #setPropeties() at the end.
var hasProperties = arg && Base.isPlainObject(arg) var hasProperties = arg && Base.isPlainObject(arg)
&& arg.x === undefined && arg.y === undefined; && arg.x === undefined && arg.y === undefined;
this.base(hasProperties ? null : Point.read(arguments)); Item.call(this, hasProperties ? null : Point.read(arguments));
this._content = ''; this._content = '';
this._lines = []; this._lines = [];
if (hasProperties) if (hasProperties)

View file

@ -51,7 +51,7 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{
// DOCS: rewrite Tool constructor explanation // DOCS: rewrite Tool constructor explanation
initialize: function(props) { initialize: function(props) {
this.base(); PaperScopeItem.call(this);
this._firstMove = true; this._firstMove = true;
this._count = 0; this._count = 0;
this._downCount = 0; this._downCount = 0;

View file

@ -34,7 +34,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
this._context = canvas.getContext('2d'); this._context = canvas.getContext('2d');
// Have Item count installed mouse events. // Have Item count installed mouse events.
this._eventCounters = {}; this._eventCounters = {};
this.base(canvas); View.call(this, canvas);
}, },
/** /**

View file

@ -22,7 +22,7 @@
*/ */
var KeyEvent = this.KeyEvent = Event.extend(/** @lends KeyEvent# */{ var KeyEvent = this.KeyEvent = Event.extend(/** @lends KeyEvent# */{
initialize: function(down, key, character, event) { initialize: function(down, key, character, event) {
this.base(event); Event.call(this, event);
this.type = down ? 'keydown' : 'keyup'; this.type = down ? 'keydown' : 'keyup';
this.key = key; this.key = key;
this.character = character; this.character = character;

View file

@ -24,7 +24,7 @@
*/ */
var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{ var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{
initialize: function(type, event, point, target, delta) { initialize: function(type, event, point, target, delta) {
this.base(event); Event.call(this, event);
this.type = type; this.type = type;
this.point = point; this.point = point;
this.target = target; this.target = target;