diff --git a/lib/class_builder.js b/lib/class_builder.js index d78a32b..d3cf71e 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -621,13 +621,18 @@ function attachStatic( ctor, members, base, inheriting ) // cause us to continue checking the parent, thereby potentially // failing to set perfectly legal values var has = Object.prototype.hasOwnProperty.call( - props[ 'public' ], prop - ); + props[ 'public' ], prop + ), + + // Determine if we were invoked in the context of a class. If + // so, use that. Otherwise, use ourself. + context = ( this.___$$sprops$$ ) ? this : ctor + ; // if we don't own the property, let the parent(s) handle it if ( !has ) { - return base.$.apply( this, arguments ); + return base.$.apply( context, arguments ); } // if a value was provided, this method should be treated as a @@ -635,7 +640,7 @@ function attachStatic( ctor, members, base, inheriting ) if ( val ) { props[ 'public' ][ prop ] = val; - return; + return context; } else { diff --git a/test/test-class_builder-static.js b/test/test-class_builder-static.js index ae82a63..360a1f3 100644 --- a/test/test-class_builder-static.js +++ b/test/test-class_builder-static.js @@ -370,3 +370,29 @@ var common = require( './common' ), ); } )(); + +/** + * Ensure that the proper context is returned by static property setters. It + * should return the calling class, regardless of whether or not it owns the + * property being requested. + */ +( function testStaticPropertySettersReturnProperContext() +{ + var Foo = builder.build( + { + 'public static foo': '', + } ), + + SubFoo = builder.build( Foo, {} ) + ; + + assert.ok( Foo.$( 'foo', 'val' ) === Foo, + "Static property setter returns self" + ); + + assert.ok( SubFoo.$( 'foo', 'val' ) === SubFoo, + "Static property setter returns calling class, even if property is " + + "owned by a supertype" + ); +} )(); +