diff --git a/lib/class_builder.js b/lib/class_builder.js index 280ac81..e3df1cd 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -211,6 +211,14 @@ exports.build = function extend() new_class.___$$props$$ = prop_init; 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 var meta = createMeta( new_class, base ); meta.abstractMethods = abstract_methods; @@ -488,10 +496,11 @@ function buildMembers( * * @param {Object} prototype prototype to attach method to * @param {Object} properties properties to initialize - * @param {number} cid class id * * @param {{public: Object, protected: Object, private: Object}} members * + * @param {number} cid class id + * * @return {undefined} */ function attachPropInit( prototype, properties, members, cid ) diff --git a/test/test-class_builder-static.js b/test/test-class_builder-static.js index e6fa8ed..77ba8f2 100644 --- a/test/test-class_builder-static.js +++ b/test/test-class_builder-static.js @@ -28,6 +28,27 @@ var common = require( './common' ), 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 * definition itself; that is, without instantiation. It should also not be