diff --git a/lib/class.js b/lib/class.js index 7963020..0b638c7 100644 --- a/lib/class.js +++ b/lib/class.js @@ -255,6 +255,10 @@ var extend = ( function( extending ) this.__construct.apply( this, ( args || arguments ) ); args = null; } + + // attach any instance properties/methods (done after + // constructor to ensure they are not overridden) + attachInstanceOf( this ); }; return __self; @@ -286,6 +290,7 @@ function setupProps( func, abstract_methods ) { attachAbstract( func, abstract_methods ); attachExtend( func ); + attachInstanceOf( func ); } @@ -384,3 +389,19 @@ function attachExtend( func ) }); } + +/** + * Attaches partially applied isInstanceOf() method to class instance + * + * @param {Object} instance class instance to attach method to + * + * @return {undefined} + */ +function attachInstanceOf( instance ) +{ + util.defineSecureProp( instance, 'isInstanceOf', function( type ) + { + return exports.isInstanceOf( type, instance ); + }); +} + diff --git a/test/test-class.js b/test/test-class.js index b9d814f..c3829e9 100644 --- a/test/test-class.js +++ b/test/test-class.js @@ -112,4 +112,11 @@ assert.ok( "Class is not an instance of its instance" ); +assert.ok( + ( ( inst.isInstanceOf instanceof Function ) + && ( inst.isInstanceOf( Foo ) === true ) + && ( inst.isInstanceOf( inst ) === false ) + ), + "Class instance contains partially applied isInstanceOf method" +);