diff --git a/lib/propobj.js b/lib/propobj.js index dba5208..536e13e 100644 --- a/lib/propobj.js +++ b/lib/propobj.js @@ -33,7 +33,8 @@ var util = require( './util' ), exports.setup = function( base, dest, properties, members ) { var prop_pub = properties[ 'public' ], - prop_prot = properties[ 'protected' ] + prop_prot = properties[ 'protected' ], + prop_priv = properties[ 'private' ] ; // initialize each of the properties for this instance to @@ -46,6 +47,7 @@ exports.setup = function( base, dest, properties, members ) } var methods_protected = members[ 'protected' ], + methods_private = members[ 'private' ], hasOwn = Array.prototype.hasOwnProperty ; @@ -57,12 +59,23 @@ exports.setup = function( base, dest, properties, members ) 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 ]; + } + } - // initialize protected properties and store in instance data + // initialize private/protected properties and store in instance data for ( prop in prop_prot ) { dest[ prop ] = util.clone( prop_prot[ prop ] ); } + for ( prop in prop_priv ) + { + dest[ prop ] = util.clone( prop_priv[ prop ] ); + } }; diff --git a/test/test-class-visibility.js b/test/test-class-visibility.js index b32fb7f..bf28361 100644 --- a/test/test-class-visibility.js +++ b/test/test-class-visibility.js @@ -199,3 +199,24 @@ var common = require( './common' ), ); } )(); + +/** + * Private members should be accessible from within class methods + */ +( function testPrivateMembersAreAccessibleInternally() +{ + assert.equal( + foo.getProp( 'parts' ), + priv, + "Private properties are available internally" + ); + + // invoke rather than checking for equality, because the method may be + // wrapped + assert.equal( + foo.getProp( 'privf' )(), + priv, + "Private methods are available internally" + ); +} )(); +