README.md (2869B)
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 # hasOwnProperty 22 23 > Test if an object has a specified property. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 31 ``` 32 33 #### hasOwnProp( value, property ) 34 35 Returns a `boolean` indicating if a `value` has a specified `property`. 36 37 ```javascript 38 var value = { 39 'beep': 'boop' 40 }; 41 42 var bool = hasOwnProp( value, 'beep' ); 43 // returns true 44 45 bool = hasOwnProp( value, 'bap' ); 46 // returns false 47 ``` 48 49 </section> 50 51 <!-- /.usage --> 52 53 <section class="notes"> 54 55 ## Notes 56 57 - In contrast to the native [Object.prototype.hasOwnProperty][mdn-object-has-own-property], this function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`. 58 59 ```javascript 60 var bool = hasOwnProp( null, 'a' ); 61 // returns false 62 63 bool = hasOwnProp( void 0, 'a' ); 64 // returns false 65 ``` 66 67 - Value arguments other than `null` or `undefined` are coerced to `objects`. 68 69 ```javascript 70 var bool = hasOwnProp( 'beep', 'length' ); 71 // returns true 72 ``` 73 74 - Property arguments are coerced to `strings`. 75 76 ```javascript 77 var value = { 78 'null': false 79 }; 80 var bool = hasOwnProp( value, null ); 81 // returns true 82 83 value = { 84 '[object Object]': false 85 }; 86 bool = hasOwnProp( value, {} ); 87 // returns true 88 ``` 89 90 </section> 91 92 <!-- /.notes --> 93 94 <section class="examples"> 95 96 ## Examples 97 98 <!-- eslint-disable object-curly-newline --> 99 100 <!-- eslint no-undef: "error" --> 101 102 ```javascript 103 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 104 105 var bool = hasOwnProp( { 'a': 'b' }, 'a' ); 106 // returns true 107 108 bool = hasOwnProp( { 'a': 'b' }, 'c' ); 109 // returns false 110 111 bool = hasOwnProp( { 'a': 'b' }, null ); 112 // returns false 113 114 bool = hasOwnProp( {}, 'hasOwnProperty' ); 115 // returns false 116 117 bool = hasOwnProp( null, 'a' ); 118 // returns false 119 120 bool = hasOwnProp( void 0, 'a' ); 121 // returns false 122 123 bool = hasOwnProp( { 'null': false }, null ); 124 // returns true 125 126 bool = hasOwnProp( { '[object Object]': false }, {} ); 127 // returns true 128 ``` 129 130 </section> 131 132 <!-- /.examples --> 133 134 <section class="links"> 135 136 [mdn-object-has-own-property]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty 137 138 </section> 139 140 <!-- /.links -->