1
0
Fork 0

Protected static methods are now inherited by subtypes

closure/master
Mike Gerwitz 2011-05-11 17:56:48 -04:00
parent d542f9d803
commit 9822894eae
2 changed files with 54 additions and 2 deletions

View File

@ -228,13 +228,13 @@ exports.build = function extend()
var new_class = createCtor( cname, abstract_methods, members ); var new_class = createCtor( cname, abstract_methods, members );
// closure to hold static initialization to be used later by subtypes // closure to hold static initialization to be used later by subtypes
initStaticVisibilityObj( new_class, static_members );
var staticInit = function( ctor, inheriting ) var staticInit = function( ctor, inheriting )
{ {
attachStatic( ctor, static_members, base, inheriting ); attachStatic( ctor, static_members, base, inheriting );
} }
staticInit( new_class, false ); staticInit( new_class, false );
initStaticVisibilityObj( new_class, static_members );
attachPropInit( prototype, prop_init, members, new_class, class_id ); attachPropInit( prototype, prop_init, members, new_class, class_id );
new_class.prototype = prototype; new_class.prototype = prototype;
@ -615,7 +615,6 @@ function initStaticVisibilityObj( ctor, static_members )
sobj.prototype = ctor; sobj.prototype = ctor;
var sobji = new sobj(); var sobji = new sobj();
util.copyTo( sobji, static_members.methods[ 'protected' ] );
// override __self on the instance's visibility object, giving internal // override __self on the instance's visibility object, giving internal
// methods access to the restricted static methods // methods access to the restricted static methods
@ -698,6 +697,7 @@ function attachStatic( ctor, members, base, inheriting )
// copy over public static methods // copy over public static methods
util.copyTo( ctor, methods[ 'public' ], true ); util.copyTo( ctor, methods[ 'public' ], true );
util.copyTo( ctor.___$$svis$$, methods[ 'protected' ], true );
} }

View File

@ -474,3 +474,55 @@ var common = require( './common' ),
); );
} )(); } )();
/**
* As usual, protected members (in this case, static) should be inherited by
* subtypes.
*/
( function testProtectedStaticMembersAreInheritedBySubtypes()
{
var val = 'baz',
val2 = 'bazbaz',
Foo = builder.build(
{
'protected static foo': function()
{
return val;
},
} ),
SubFoo = builder.build( Foo,
{
'public static bar': function()
{
return this.foo();
},
'protected static foo2': function()
{
return val2;
},
'public static bar2': function()
{
return this.foo2();
},
} ),
SubSubFoo = builder.build( SubFoo, {} )
;
assert.equal( SubFoo.bar(), val,
"Subtypes inherit parents' protected static methods"
);
assert.equal( SubFoo.bar2(), val2,
"Static methods have access to other static methods in the same class"
);
// for extra assurance, to ensure our recursive implementation is correct
assert.equal( SubSubFoo.bar(), val,
"Sub-subtypes inherit parents' protected static methods"
);
} )();