Freezing class object after creation
parent
0b4ec19911
commit
0e232b4a2c
21
lib/class.js
21
lib/class.js
|
@ -22,12 +22,24 @@
|
|||
* @package core
|
||||
*/
|
||||
|
||||
// whether getters/setters are supported
|
||||
/**
|
||||
* Whether getters/setters are supported
|
||||
* @var {boolean}
|
||||
*/
|
||||
var getset = ( Object.prototype.__defineGetter__ === undefined )
|
||||
? false
|
||||
: true
|
||||
;
|
||||
|
||||
/**
|
||||
* Whether the Object.freeze() method is available
|
||||
* @var {boolean}
|
||||
*/
|
||||
var can_freeze = ( Object.seal === undefined )
|
||||
? false
|
||||
: true
|
||||
;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a class, inheriting either from the provided base class or the
|
||||
|
@ -133,6 +145,13 @@ var extend = ( function( extending )
|
|||
new_class.prototype = prototype;
|
||||
new_class.constructor = new_class;
|
||||
|
||||
// lock down the new class (if supported) to ensure that we can't add
|
||||
// members at runtime
|
||||
if ( can_freeze )
|
||||
{
|
||||
Object.freeze( new_class );
|
||||
}
|
||||
|
||||
// we're done with the extension process
|
||||
extending = false;
|
||||
|
||||
|
|
|
@ -34,7 +34,10 @@ assert.ok(
|
|||
);
|
||||
|
||||
|
||||
var Foo = Class.extend();
|
||||
var Foo = Class.extend(
|
||||
{
|
||||
value: 'foo',
|
||||
});
|
||||
|
||||
|
||||
assert.ok(
|
||||
|
@ -42,3 +45,8 @@ assert.ok(
|
|||
"Extend method creates a new object"
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
Object.isFrozen( Foo ),
|
||||
true,
|
||||
"Generated class should be frozen"
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue