1
0
Fork 0

Refactored propbj.setup() to be a bit more managable

closure/master
Mike Gerwitz 2011-03-06 23:56:19 -05:00
parent 30bfde50cd
commit d1be2d5351
2 changed files with 42 additions and 30 deletions

View File

@ -774,14 +774,20 @@ function initInstance( iid, instance )
*/ */
function attachPropInit( prototype, properties, members ) function attachPropInit( prototype, properties, members )
{ {
util.defineSecureProp( prototype, '__initProps', function() util.defineSecureProp( prototype, '__initProps', function( inherit )
{ {
// defaults to false
inherit = !!inherit;
// first initialize the parent's properties, so that ours will overwrite // first initialize the parent's properties, so that ours will overwrite
// them // them
var parent_init = prototype.parent.__initProps; var parent_init = prototype.parent.__initProps;
if ( parent_init instanceof Function ) if ( parent_init instanceof Function )
{ {
parent_init.call( this ); // call the parent prop_init, letting it know that it's been
// inherited so that it does not initialize private members or
// perform other unnecessary tasks
parent_init.call( this, true );
} }
var inst_props = propobj.createPropProxy( var inst_props = propobj.createPropProxy(

View File

@ -34,49 +34,55 @@ exports.setup = function( base, dest, properties, members )
{ {
var prop_pub = properties[ 'public' ], var prop_pub = properties[ 'public' ],
prop_prot = properties[ 'protected' ], prop_prot = properties[ 'protected' ],
prop_priv = properties[ 'private' ] prop_priv = properties[ 'private' ],
methods_protected = members[ 'protected' ],
methods_private = members[ 'private' ]
; ;
// initialize each of the properties for this instance to // initialize each of the properties for this instance to
// ensure we're not sharing references to prototype values // ensure we're not sharing references to prototype values
for ( prop in prop_pub ) doSetup( dest, prop_pub );
{
// initialize the value with a clone to ensure that they do
// not share references (and therefore, data)
base[ prop ] = util.clone( prop_pub[ prop ] );
}
var methods_protected = members[ 'protected' ], doSetup( dest, prop_prot, methods_protected );
methods_private = members[ 'private' ], doSetup( dest, prop_priv, methods_private );
hasOwn = Array.prototype.hasOwnProperty };
;
/**
* Set up destination object by copying over properties and methods
*
* @param {Object} dest destination object
* @param {Object} properties properties to copy
* @param {Object=} methods methods to copy
*
* @return {undefined}
*/
function doSetup( dest, properties, methods )
{
var hasOwn = Array.prototype.hasOwnProperty;
// copy over the methods // copy over the methods
for ( method_name in methods_protected ) if ( methods !== undefined )
{ {
if ( hasOwn.call( methods_protected, method_name ) ) for ( method_name in methods )
{ {
dest[ method_name ] = methods_protected[ method_name ]; if ( hasOwn.call( methods, method_name ) )
} {
} dest[ method_name ] = methods[ method_name ];
for ( method_name in methods_private ) }
{
if ( hasOwn.call( methods_private, method_name ) )
{
dest[ method_name ] = methods_private[ method_name ];
} }
} }
// initialize private/protected properties and store in instance data // initialize private/protected properties and store in instance data
for ( prop in prop_prot ) for ( prop in properties )
{ {
dest[ prop ] = util.clone( prop_prot[ prop ] ); if ( hasOwn.call( properties, prop ) )
{
dest[ prop ] = util.clone( properties[ prop ] );
}
} }
for ( prop in prop_priv ) }
{
dest[ prop ] = util.clone( prop_priv[ prop ] );
}
};
/** /**