1
0
Fork 0

Const keyword now results in static property

closure/master
Mike Gerwitz 2011-05-19 19:07:31 -04:00
parent 030bdef821
commit 2d91a221aa
2 changed files with 41 additions and 4 deletions

View File

@ -470,7 +470,7 @@ function buildMembers(
property: function( name, value, keywords )
{
var dest = ( keywords[ 'static' ] ) ? sprops : prop_init;
var dest = ( keywordStatic( keywords ) ) ? sprops : prop_init;
// build a new property, passing in the other members to compare
// against for preventing nonsensical overrides
@ -481,7 +481,7 @@ function buildMembers(
getter: function( name, value, keywords )
{
var dest = ( keywords[ 'static' ] ) ? smethods : members;
var dest = ( keywordStatic( keywords ) ) ? smethods : members;
member_builder.buildGetter(
dest, null, name, value, keywords
@ -490,7 +490,7 @@ function buildMembers(
setter: function( name, value, keywords )
{
var dest = ( keywords[ 'static' ] ) ? smethods : members;
var dest = ( keywordStatic( keywords ) ) ? smethods : members;
member_builder.buildSetter(
dest, null, name, value, keywords
@ -499,7 +499,7 @@ function buildMembers(
method: function( name, func, is_abstract, keywords )
{
var dest = ( keywords[ 'static' ] ) ? smethods : members;
var dest = ( keywordStatic( keywords ) ) ? smethods : members;
// constructor check
if ( public_methods[ name ] === true )
@ -536,6 +536,24 @@ function buildMembers(
}
/**
* Determines if the given keywords should result in a static member
*
* A member will be considered static if the static or const keywords are given.
*
* @param {Object} keywords keywords to scan
*
* @return {bool} true if to be static, otherwise false
*/
function keywordStatic( keywords )
{
return ( keywords[ 'static' ] || keywords[ 'const' ] )
? true
: false
;
}
/**
* Attaches __initProps() method to the class prototype
*

View File

@ -84,3 +84,22 @@ var common = require( './common' ),
assert.fail( "Should not be able to use static keyword with const" );
} )();
/**
* The const keyword should result in a static property. The rationale for this
* is that, if a value is constant, then instances do not make sense.
*/
( function testConstKeywordDeclaresPropertiesAsStatic()
{
var val = 'baz',
Foo = builder.build(
{
'const foo': val,
} )
;
assert.equal( val, Foo.$('foo'),
"Const keyword should declare properties as static"
);
} )();