Implemented constant properties
parent
2d91a221aa
commit
6e1d796092
|
@ -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 ];
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
|
|
@ -235,7 +235,7 @@ exports.buildProp = function( members, meta, name, value, keywords, base )
|
|||
);
|
||||
}
|
||||
|
||||
getMemberVisibility( members, keywords )[ name ] = value;
|
||||
getMemberVisibility( members, keywords )[ name ] = [ value, keywords ];
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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 ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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" );
|
||||
} )();
|
||||
|
||||
|
|
Loading…
Reference in New Issue