1
0
Fork 0

Implemented constant properties

closure/master
Mike Gerwitz 2011-05-19 19:48:47 -04:00
parent 2d91a221aa
commit 6e1d796092
4 changed files with 38 additions and 4 deletions

View File

@ -745,18 +745,28 @@ function attachStatic( ctor, members, base, inheriting )
return base.$.apply( context, arguments );
}
var prop_item = props[ found ][ prop ];
// if a value was provided, this method should be treated as a
// setter rather than a getter (we *must* test using
// arguments.length to ensure that setting to undefined works)
if ( arguments.length > 1 )
{
props[ found ][ prop ] = val;
// if const, disallow modification
if ( prop_item[ 1 ][ 'const' ] )
{
throw TypeError(
"Cannot modify constant property '" + prop + "'"
);
}
prop_item[ 0 ] = val;
return context;
}
else
{
// return the value
return props[ found ][ prop ];
return prop_item[ 0 ];
}
} );
}

View File

@ -235,7 +235,7 @@ exports.buildProp = function( members, meta, name, value, keywords, base )
);
}
getMemberVisibility( members, keywords )[ name ] = value;
getMemberVisibility( members, keywords )[ name ] = [ value, keywords ];
};

View File

@ -137,7 +137,7 @@ function doSetup( dest, properties, methods, unless_keyword )
{
if ( hasOwn.call( properties, prop ) )
{
dest[ prop ] = util.clone( properties[ prop ] );
dest[ prop ] = util.clone( properties[ prop ][ 0 ] );
}
}
}

View File

@ -103,3 +103,27 @@ var common = require( './common' ),
);
} )();
/**
* As the name implies, constant properties should not be writable.
*/
( function testConstKeywordCreatesImmutableProperty()
{
try
{
// this should fail (trying to alter const prop foo)
builder.build( { 'const foo': 'bar' } ).$( 'foo', 'baz' );
}
catch ( e )
{
assert.ok(
e.message.search( 'foo' ) !== -1,
"Const modification error should contain name of property"
);
return;
}
assert.fail( "Constant properties should not be writable" );
} )();