@stdlib/symbol-has-instance
v0.1.1
Published
Has instance symbol.
Downloads
17,717
Readme
HasInstanceSymbol
Has instance symbol which is used to determine if a constructor object recognizes an object as its instance.
Installation
npm install @stdlib/symbol-has-instanceUsage
var HasInstanceSymbol = require( '@stdlib/symbol-has-instance' );HasInstanceSymbol
Has instance symbol which is used to determine if a constructor object recognizes an object as its instance.
var s = typeof HasInstanceSymbol;
// e.g., returns 'symbol'Notes
The symbol is only supported in environments which support symbols. In non-supporting environments, the value is
null.The
instanceofoperator uses the following algorithm to determine the return value ofobject instanceof constructor:- If
constructorhas a[HasInstanceSymbol]()method, theinstanceofoperator calls the method withobjectas the first argument and returns the result (coerced to a boolean). Ifconstructoris not an object or ifconstructor[HasInstanceSymbol]is neithernull,undefined, nor a function, theinstanceofoperator raises an exception. - Otherwise, if
constructordoes not have a[HasInstanceSymbol]()method (i.e.,constructor[HasInstanceSymbol]isnullorundefined), theinstanceofoperator determines the result using the same algorithm asFunction.prototype[HasInstanceSymbol](). Ifconstructoris not a function, theinstanceofoperator raises an exception.
- If
Examples
var isArray = require( '@stdlib/assert-is-array' );
var instanceOf = require( '@stdlib/assert-instance-of' );
var defineProperty = require( '@stdlib/utils-define-property' );
var HasInstanceSymbol = require( '@stdlib/symbol-has-instance' );
function ArrayLike() {
return {
'length': 3,
'0': 4,
'1': 5,
'2': 6
};
}
function hasInstance( instance ) {
return isArray( instance );
}
var x = [ 1, 2, 3 ];
defineProperty( ArrayLike, HasInstanceSymbol, {
'configurable': true,
'value': null
});
console.log( instanceOf( x, ArrayLike ) );
defineProperty( ArrayLike, HasInstanceSymbol, {
'configurable': true,
'value': hasInstance
});
console.log( instanceOf( x, ArrayLike ) );Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
