1
0
Fork 0

Initial concept of inheriting protected/private members

closure/master
Mike Gerwitz 2011-03-07 00:14:43 -05:00
parent f692ebcdfd
commit e05a65d5fa
2 changed files with 67 additions and 3 deletions

View File

@ -794,9 +794,17 @@ function attachPropInit( prototype, properties, members )
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; class_instance[ this.__iid ] = inst_props;
propobj.setup( inst_props, properties, members ); // if we're inheriting, perform a setup that doesn't include everything
// that we don't want (e.g. private properties)
var setup = ( inherit )
? propobj.setupInherited
: propobj.setup
;
setup( inst_props, properties, members );
}); });
} }

View File

@ -36,7 +36,7 @@ var common = require( './common' ),
privf = function() { return priv; }, privf = function() { return priv; },
// new anonymous class instance // new anonymous class instance
foo = Class.extend( { Foo = Class.extend( {
'public pub': pub, 'public pub': pub,
'protected peeps': prot, 'protected peeps': prot,
'private parts': priv, 'private parts': priv,
@ -60,7 +60,14 @@ var common = require( './common' ),
{ {
this[ name ] = value; this[ name ] = value;
}, },
})(); }),
// instance of Foo
foo = Foo(),
// subtype
sub_foo = Foo.extend( {} )()
;
/** /**
@ -220,3 +227,52 @@ var common = require( './common' ),
); );
} )(); } )();
/**
* Inheritance 101; protected members should be available to subtypes
*/
( function testProtectedMembersAreInheritedFromParent()
{
assert.equal(
sub_foo.getProp( 'peeps' ),
prot,
"Protected properties are available to subtypes"
);
// invoke rather than checking for equality, because the method may be
// wrapped
assert.equal(
sub_foo.getProp( 'protf' )(),
prot,
"Protected methods are available to subtypes"
);
} )();
/**
* Interface 101-2: We do not want private members to be available to subtypes.
*/
( function testPrivateMembersOfSupertypesAreInaccessibleToSubtypes()
{
// browsers that do not support the property proxy will not support
// encapsulating properties
if ( !( propobj.supportsPropProxy() ) )
{
return;
}
assert.equal(
sub_foo.getProp( 'parts' ),
undefined,
"Private properties of supertypes should be unavailable to subtypes"
);
// invoke rather than checking for equality, because the method may be
// wrapped
assert.equal(
sub_foo.getProp( 'privf' ),
undefined,
"Private methods of supertypes should be unavailable to subtypes"
);
} )();