README.md (2863B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # instanceOf 22 23 > Test whether a value has in its prototype chain a specified constructor as a prototype property. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var instanceOf = require( '@stdlib/assert/instance-of' ); 37 ``` 38 39 #### instanceOf( value, constructor ) 40 41 Tests whether a `value` has in its prototype chain a specified `constructor` as a `prototype` property. 42 43 ```javascript 44 var inherit = require( '@stdlib/utils/inherit' ); 45 46 function Foo() { 47 return this; 48 } 49 50 function Bar() { 51 return this; 52 } 53 inherit( Bar, Foo ); 54 55 var bar = new Bar(); 56 57 var bool = instanceOf( bar, Foo ); 58 // returns true 59 ``` 60 61 </section> 62 63 <!-- /.usage --> 64 65 <section class="notes"> 66 67 ## Notes 68 69 - The function throws a `TypeError` if provided a `constructor` argument which is not callable. 70 71 ```javascript 72 var bool = instanceOf( {}, null ); 73 // throws <TypeError> 74 ``` 75 76 - While the prototype of an `object` created using object literal notion is `undefined`, the function returns `true` when provided an `object` literal and the `Object` constructor. This maintains consistent behavior with the `instanceof` operator. 77 78 ```javascript 79 var bool = ( {} instanceof Object ); 80 // returns true 81 82 bool = instanceOf( {}, Object ); 83 // returns true 84 ``` 85 86 </section> 87 88 <!-- /.notes --> 89 90 <section class="examples"> 91 92 ## Examples 93 94 <!-- eslint no-undef: "error" --> 95 96 ```javascript 97 var Number = require( '@stdlib/number/ctor' ); 98 var instanceOf = require( '@stdlib/assert/instance-of' ); 99 100 var bool = instanceOf( [], Array ); 101 // returns true 102 103 bool = instanceOf( [], Object ); 104 // returns true 105 106 bool = instanceOf( {}, Object ); 107 // returns true 108 109 bool = instanceOf( new Date(), Date ); 110 // returns true 111 112 bool = instanceOf( /.*/, RegExp ); 113 // returns true 114 115 bool = instanceOf( instanceOf, Function ); 116 // returns true 117 118 bool = instanceOf( null, Object ); 119 // returns false 120 121 bool = instanceOf( 5, Number ); 122 // returns false 123 124 bool = instanceOf( '5', String ); 125 // returns false 126 127 bool = instanceOf( void 0, Object ); 128 // returns false 129 130 bool = instanceOf( {}, Array ); 131 // returns false 132 133 bool = instanceOf( {}, Function ); 134 // returns false 135 ``` 136 137 </section> 138 139 <!-- /.examples --> 140 141 <section class="links"> 142 143 </section> 144 145 <!-- /.links -->