1
0
Fork 0

Began implementing static property method (getter)

closure/master
Mike Gerwitz 2011-04-13 22:38:05 -04:00
parent 143348d390
commit 44cd8db82d
2 changed files with 48 additions and 15 deletions

View File

@ -105,6 +105,20 @@ var enum_bug = (
*/ */
exports.ClassBase = function Class() {}; exports.ClassBase = function Class() {};
/**
* Default static property method
*
* This simply returns undefined, signifying that the property was not found.
*
* @param {string} prop requested property
*
* @return {undefined}
*/
exports.ClassBase.$ = function( prop )
{
return undefined;
};
/** /**
* Returns a hash of the reserved members * Returns a hash of the reserved members
@ -598,11 +612,16 @@ function attachStatic( ctor, members, base, inheriting )
// initialize static property if not yet defined // initialize static property if not yet defined
if ( !inheriting ) if ( !inheriting )
{ {
// "inherit" properties from the supertype, if available ctor.___$$sprops$$ = props;
ctor.$ = base.$ || {};
// add our own properties ctor.$ = function( prop )
util.copyTo( ctor.$, props[ 'public' ], true ); {
// attempt to return property if it is our own, otherwise look up on
// parent (the parent will look up its parent as well, if necessary)
return props[ 'public' ][ prop ]
|| base.$( prop )
;
};
} }
// copy over public static members (deep copy; we don't want subtypes to // copy over public static members (deep copy; we don't want subtypes to

View File

@ -49,6 +49,23 @@ var common = require( './common' ),
} )(); } )();
/**
* If a static property does not exist, the getter should return undefined.
*
* This test exists to ensure an error is not thrown if the property is not
* found. This is because we check each parent and eventually reach the base
* object. We must ensure the base object does not cause any problems.
*/
( function testStaticPropertyLookupReturnsUndefinedIfNotFound()
{
var result = builder.build( {} ).$( 'foo' );
assert.equal( result, undefined,
"Static property getter should return undefined if not found"
);
} )();
/** /**
* Static members, by their nature, should be accessible through the class * Static members, by their nature, should be accessible through the class
* definition itself; that is, without instantiation. It should also not be * definition itself; that is, without instantiation. It should also not be
@ -79,13 +96,13 @@ var common = require( './common' ),
} ); } );
// properties should be accessible via class definition // properties should be accessible via class definition
assert.equal( Foo.$.foo, val, assert.equal( Foo.$('foo'), val,
"Public static properties should be accessible via class definition" "Public static properties should be accessible via class definition"
); );
// as long as the above test succeeded, we can then conclude that static // as long as the above test succeeded, we can then conclude that static
// members are public by default if the following succeeds // members are public by default if the following succeeds
assert.equal( Foo.$.bar, val2, assert.equal( Foo.$('bar'), val2,
"Static properties are public by default" "Static properties are public by default"
); );
@ -99,13 +116,10 @@ var common = require( './common' ),
"Static methods are public by default" "Static methods are public by default"
); );
// neither should be a part of the prototype // getter/setter method should not be a part of the prototype
assert.equal( Foo.prototype.foo, undefined, assert.equal( Foo.prototype.$, undefined,
"Public static properties are *not* part of the prototype" "Public static properties are *not* part of the prototype"
); );
assert.equal( Foo.prototype.baz, undefined,
"Public static methods are *not* part of the prototype"
);
} )(); } )();
@ -240,10 +254,10 @@ var common = require( './common' ),
; ;
// properties // properties
assert.equal( SubFoo.$.foo, Foo.$.foo, assert.equal( SubFoo.$('foo'), Foo.$('foo'),
"Public static properties are inherited by subtypes" "Public static properties are inherited by subtypes"
); );
assert.equal( SubSubFoo.$.foo, Foo.$.foo, assert.equal( SubSubFoo.$('foo'), Foo.$('foo'),
"Public static properties are inherited by sub-subtypes" "Public static properties are inherited by sub-subtypes"
); );
@ -256,7 +270,7 @@ var common = require( './common' ),
); );
// merge // merge
assert.equal( SubFoo.$.baz, baz, assert.equal( SubFoo.$('baz'), baz,
"Subtypes contain both inherited static members as well as their own" "Subtypes contain both inherited static members as well as their own"
); );
@ -303,7 +317,7 @@ var common = require( './common' ),
; ;
// the properties should reference the same object // the properties should reference the same object
assert.ok( SubFoo.$.bar === Foo.$.bar, assert.ok( SubFoo.$('bar') === Foo.$('bar'),
"Inherited static properties should share references" "Inherited static properties should share references"
); );
} )(); } )();