diff --git a/lib/class.js b/lib/class.js index 1ba3f20..ee6fd57 100644 --- a/lib/class.js +++ b/lib/class.js @@ -774,14 +774,20 @@ function initInstance( iid, instance ) */ 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 // them var parent_init = prototype.parent.__initProps; 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( diff --git a/lib/propobj.js b/lib/propobj.js index 536e13e..cf5e122 100644 --- a/lib/propobj.js +++ b/lib/propobj.js @@ -34,49 +34,55 @@ exports.setup = function( base, dest, properties, members ) { var prop_pub = properties[ 'public' ], 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 // ensure we're not sharing references to prototype values - for ( prop in 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 ] ); - } + doSetup( dest, prop_pub ); - var methods_protected = members[ 'protected' ], - methods_private = members[ 'private' ], - hasOwn = Array.prototype.hasOwnProperty - ; + doSetup( dest, prop_prot, methods_protected ); + doSetup( dest, prop_priv, methods_private ); +}; + + +/** + * 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 - 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 ]; - } - } - for ( method_name in methods_private ) - { - if ( hasOwn.call( methods_private, method_name ) ) - { - dest[ method_name ] = methods_private[ method_name ]; + if ( hasOwn.call( methods, method_name ) ) + { + dest[ method_name ] = methods[ method_name ]; + } } } // 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 ] ); - } -}; +} /**