1
0
Fork 0

Disallowed use of final keyword with properties

closure/master
Mike Gerwitz 2011-05-18 20:51:02 -04:00
parent 5182ed3e31
commit 0c5293991c
2 changed files with 42 additions and 0 deletions

View File

@ -208,6 +208,16 @@ exports.buildProp = function( members, meta, name, value, keywords, base )
);
}
// nor do final properties, because of late static binding (const should be
// used instead; read up on the rationale in the documentation)
if ( keywords[ 'final' ] )
{
throw TypeError(
"Property '" + name + "' cannot be declared as final; consider " +
"const"
);
}
getMemberVisibility( members, keywords )[ name ] = value;
};

View File

@ -61,3 +61,35 @@ var common = require( './common' ),
assert.fail( 'Should not be able to override final methods' );
} )();
/**
* Due to our static implementation (late static binding), the use of 'final'
* with properties doesn't make much sense. We have to deal with different
* problems than languages like Java that truly bind members statically. Consult
* the documentation for rationale.
*
* See also const keyword.
*/
( function testFinalKeywordCannotBeUsedWithProperties()
{
try
{
// attempt to define a 'final' property (should fail)
builder.build(
{
'final public foo': 'bar',
} );
}
catch ( e )
{
assert.ok(
e.message.search( 'foo' ) !== -1,
"Final property error message contains name of method"
);
return;
}
assert.fail( "Should not be able to use final keyword with properties" );
} )();