Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

HasInstanceSymbol

Has instance symbol which is used to determine if a constructor object recognizes an object as its instance.

Usage

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 instanceof operator uses the following algorithm to determine the return value of object instanceof constructor:

    • If constructor has a [HasInstanceSymbol]() method, the instanceof operator calls the method with object as the first argument and returns the result (coerced to a boolean). If constructor is not an object or if constructor[HasInstanceSymbol] is neither null, undefined, nor a function, the instanceof operator raises an exception.
    • Otherwise, if constructor does not have a [HasInstanceSymbol]() method (i.e., constructor[HasInstanceSymbol] is null or undefined), the instanceof operator determines the result using the same algorithm as Function.prototype[HasInstanceSymbol](). If constructor is not a function, the instanceof operator raises an exception.

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 ) );