Added isInstanceOf() partially applied function to class instances
parent
83cecb5fdc
commit
6a24f2ad13
21
lib/class.js
21
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 );
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue