1.0.10 • Published 5 years ago
@kingjs/has-instance v1.0.10
@kingjs/has-instance
Symbol.hasInstance
implementation that tests for a function's id in an instance's polymorphisms.
Usage
Test that FooBar
implements IFoo
and IBar
using instanceof
operator like this:
var hasInstance = require('@kingjs/has-instance');
var assert = require('assert')
var Identity = Symbol.for('@kingjs/Identity');
var Polymorphisms = Symbol.for('@kingjs/Polymorphisms');
function IFoo() { }
IFoo.prototype = null;
IFoo[Identity] = Symbol('IFoo')
IFoo.foo = Symbol('foo');
Object.defineProperty(IFoo, Symbol.hasInstance, { value: hasInstance });
function IBar() { }
IBar.prototype = null;
IBar[Identity] = Symbol('IBar')
IBar.bar = Symbol('bar');
Object.defineProperty(IBar, Symbol.hasInstance, { value: hasInstance });
function FooBar() { }
FooBar.prototype[IFoo.foo] = 'foo';
FooBar.prototype[IBar.bar] = 'bar';
FooBar[Polymorphisms] = {
[IFoo[Identity]]: IFoo,
[IBar[Identity]]: IBar,
}
var fooBar = new FooBar();
var isIFoo = fooBar instanceof IFoo;
var isIBar = fooBar instanceof IBar;
assert(isIFoo);
assert(isIBar);
See Also
Identity
: see @kingjs/identityPolymorphisms
: see @kingjs/polymorphisms
Install
With npm installed, run
$ npm install @kingjs/has-instance
License
MIT