1.0.10 • Published 5 years ago

@kingjs/has-instance v1.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

@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

Install

With npm installed, run

$ npm install @kingjs/has-instance

License

MIT

Analytics

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago