1
0
Fork 0

Implemented private static methods

closure/master
Mike Gerwitz 2011-05-11 20:10:10 -04:00
parent a303adddea
commit fa8d1bebe1
2 changed files with 74 additions and 0 deletions

View File

@ -700,6 +700,12 @@ 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 ); util.copyTo( ctor.___$$svis$$, methods[ 'protected' ], true );
// private methods should not be inherited by subtypes
if ( !inheriting )
{
util.copyTo( ctor.___$$svis$$, methods[ 'private' ], true );
}
} }

View File

@ -527,6 +527,74 @@ var common = require( './common' ),
} )(); } )();
/**
* Private members should be available from within the class, but not outside of
* it
*/
( function testPrivateStaticMembersAreAvailableInsideClassOnly()
{
var val = 'foo',
Foo = builder.build(
{
// the same rules should apply to methods
'private static baz': function()
{
return val;
},
// ensure method is accessible to static methods
'public static staticBaz': function()
{
return this.baz();
},
// ensure method is accessible to instance methods
'public instBaz': function()
{
return this.__self.baz();
},
} );
assert.equal( Foo.baz, undefined,
"Private methods should not be accessible outside the class"
);
assert.equal( Foo.staticBaz(), val,
"Private methods are accessible to static methods"
);
assert.equal( Foo().instBaz(), val,
"Private methods are accessible to instance methods"
);
} )();
/**
* Private static members should not be inherited by subtypes. Of course. Moving
* along...
*/
( function testPrivateStaticMembersAreNotInheritedBySubtypes()
{
var Foo = builder.build(
{
'private static priv': function() {},
} ),
SubFoo = builder.build( Foo,
{
'public static getPriv': function()
{
return this.priv;
},
} )
;
assert.equal( SubFoo.getPriv(), undefined,
"Private static methods should not be inherited by subtypes"
);
} )();
/** /**
* Public and protected static methods should be able to be overridden by * Public and protected static methods should be able to be overridden by
* subtypes. We needn't test private methods, as they are not inherited. * subtypes. We needn't test private methods, as they are not inherited.