1
0
Fork 0

Implemented __self for static access

closure/master
Mike Gerwitz 2011-04-05 22:07:13 -04:00
parent ea0d18d8eb
commit 7560d57619
2 changed files with 31 additions and 1 deletions

View File

@ -211,6 +211,14 @@ exports.build = function extend()
new_class.___$$props$$ = prop_init; new_class.___$$props$$ = prop_init;
new_class.___$$methods$$ = members; new_class.___$$methods$$ = members;
// We reduce the overall cost of this definition by defining it on the
// prototype rather than during instantiation. While this does increase the
// amount of time it takes to access the property through the prototype
// chain, it takes much more time to define the property in this manner.
// Therefore, we can save a substantial amount of time by defining it on the
// prototype rather than on each new instance via __initProps().
util.defineSecureProp( prototype, '__self', new_class );
// create internal metadata for the new class // create internal metadata for the new class
var meta = createMeta( new_class, base ); var meta = createMeta( new_class, base );
meta.abstractMethods = abstract_methods; meta.abstractMethods = abstract_methods;
@ -488,10 +496,11 @@ function buildMembers(
* *
* @param {Object} prototype prototype to attach method to * @param {Object} prototype prototype to attach method to
* @param {Object} properties properties to initialize * @param {Object} properties properties to initialize
* @param {number} cid class id
* *
* @param {{public: Object, protected: Object, private: Object}} members * @param {{public: Object, protected: Object, private: Object}} members
* *
* @param {number} cid class id
*
* @return {undefined} * @return {undefined}
*/ */
function attachPropInit( prototype, properties, members, cid ) function attachPropInit( prototype, properties, members, cid )

View File

@ -28,6 +28,27 @@ var common = require( './common' ),
fallback = common.require( 'util' ).definePropertyFallback() fallback = common.require( 'util' ).definePropertyFallback()
; ;
/**
* To provide access to static members, this.__self is made available inside of
* instances.
*/
( function testSelfPropertyReferencesClassDefinition()
{
var Foo = builder.build(
{
'public function test': function()
{
return this.__self;
},
} );
assert.deepEqual( Foo().test(), Foo,
"__self property references class definition"
);
} )();
/** /**
* 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