1
0
Fork 0

Added isInstanceOf() partially applied function to class instances

closure/master
Mike Gerwitz 2010-12-29 21:32:16 -05:00
parent 83cecb5fdc
commit 6a24f2ad13
2 changed files with 28 additions and 0 deletions

View File

@ -255,6 +255,10 @@ var extend = ( function( extending )
this.__construct.apply( this, ( args || arguments ) ); this.__construct.apply( this, ( args || arguments ) );
args = null; args = null;
} }
// attach any instance properties/methods (done after
// constructor to ensure they are not overridden)
attachInstanceOf( this );
}; };
return __self; return __self;
@ -286,6 +290,7 @@ function setupProps( func, abstract_methods )
{ {
attachAbstract( func, abstract_methods ); attachAbstract( func, abstract_methods );
attachExtend( func ); 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 );
});
}

View File

@ -112,4 +112,11 @@ assert.ok(
"Class is not an instance of its instance" "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"
);