1
0
Fork 0

Separated private members into a separate object (propobj) to prepare for future modifications

- This incurs a performance hit for accessing protected members, and even further for public, internally
  - But speeds up access to private members, likely due to there being less members
closure/master
Mike Gerwitz 2011-03-12 23:48:38 -05:00
parent 47a6ba2727
commit 6b374902ae
3 changed files with 25 additions and 9 deletions

View File

@ -790,21 +790,19 @@ function attachPropInit( prototype, properties, members )
parent_init.call( this, true ); parent_init.call( this, true );
} }
// this will return our property proxy, if supported by our environment,
// otherwise just a normal object with everything merged in
var inst_props = propobj.createPropProxy( var inst_props = propobj.createPropProxy(
this, class_instance[ this.__iid ], properties[ 'public' ] this, class_instance[ this.__iid ], properties[ 'public' ]
); );
// use whatever was returned by the property proxy (which may not be a
// proxy, depending on JS engine support)
class_instance[ this.__iid ] = inst_props;
// if we're inheriting, perform a setup that doesn't include everything // if we're inheriting, perform a setup that doesn't include everything
// that we don't want (e.g. private properties) // that we don't want (e.g. private properties)
var setup = ( inherit ) var setup = ( inherit )
? propobj.setupInherited ? propobj.setupInherited
: propobj.setup : propobj.setup
; ;
setup( inst_props, properties, members ); class_instance[ this.__iid ] = setup( inst_props, properties, members );
}); });
} }

View File

@ -39,7 +39,7 @@ var util = require( './util' ),
* @param {Object} properties properties to copy * @param {Object} properties properties to copy
* @param {Object=} methods methods to copy * @param {Object=} methods methods to copy
* *
* @return {undefined} * @return {Object} dest
*/ */
exports.setupInherited = function( dest, properties, methods ) exports.setupInherited = function( dest, properties, methods )
{ {
@ -47,27 +47,41 @@ exports.setupInherited = function( dest, properties, methods )
// ensure we're not sharing references to prototype values // ensure we're not sharing references to prototype values
doSetup( dest, properties[ 'public' ] ); doSetup( dest, properties[ 'public' ] );
doSetup( dest, properties[ 'protected' ], methods[ 'protected'] ); doSetup( dest, properties[ 'protected' ], methods[ 'protected'] );
return dest;
}; };
/** /**
* Sets up properties (non-inheriting) * Sets up properties (non-inheriting)
* *
* This includes all members (including private). * This includes all members (including private). Private members will be set up
* in a separate object, so that they can be easily removed from the mix. That
* object will include the destination object in the prototype, so that the
* access should be transparent. This object is returned.
* *
* @param {Object} dest destination object * @param {Object} dest destination object
* @param {Object} properties properties to copy * @param {Object} properties properties to copy
* @param {Object=} methods methods to copy * @param {Object=} methods methods to copy
* *
* @return {undefined} * @return {Object} object containing private members and dest as prototype
*/ */
exports.setup = function( dest, properties, methods ) exports.setup = function( dest, properties, methods )
{ {
// this constructor is an extra layer atop of the destination object, which
// will contain the private methods
var obj_ctor = function() {};
obj_ctor.prototype = dest;
var obj = new obj_ctor();
// first, set up the public and protected members // first, set up the public and protected members
exports.setupInherited( dest, properties, methods ); exports.setupInherited( dest, properties, methods );
// then add the private parts // then add the private parts
doSetup( dest, properties[ 'private' ], methods[ 'private' ] ); doSetup( obj, properties[ 'private' ], methods[ 'private' ] );
return obj;
}; };

View File

@ -13,6 +13,10 @@
* - Protected and private methods internally should be accessed fairly * - Protected and private methods internally should be accessed fairly
* quickly since, like public methods externally, they are first on the * quickly since, like public methods externally, they are first on the
* prototype chain. * prototype chain.
* - Protected members will be accessed more slowly than private members,
* because they are one step lower on the prototype chain. Future versions
* will remove this performance hit if the Class contains no private
* members.
* *
* Copyright (C) 2010 Mike Gerwitz * Copyright (C) 2010 Mike Gerwitz
* *