README.md (2822B)
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 # inheritedKeys 22 23 > Return an array of an object's inherited enumerable property names. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var inheritedKeys = require( '@stdlib/utils/inherited-keys' ); 31 ``` 32 33 #### inheritedKeys( obj\[, level] ) 34 35 Returns an `array` of an object's inherited enumerable property names. 36 37 ```javascript 38 function Foo() { 39 this.a = 'b'; 40 return this; 41 } 42 43 Foo.prototype.beep = 'boop'; 44 45 var f = new Foo(); 46 var keys = inheritedKeys( f ); 47 // returns [ 'beep' ] 48 ``` 49 50 By default, the function walks an object's entire prototype chain. To limit the inheritance level, provide a `level` argument. 51 52 ```javascript 53 var inherit = require( '@stdlib/utils/inherit' ); 54 55 function Bar() { 56 return this; 57 } 58 59 Bar.prototype.boop = 'beep'; 60 61 function Foo() { 62 Bar.call( this ); 63 this.a = 'b'; 64 return this; 65 } 66 67 inherit( Foo, Bar ); 68 Foo.prototype.beep = 'boop'; 69 70 var f = new Foo(); 71 var keys = inheritedKeys( f, 1 ); 72 // returns [ 'beep' ] 73 ``` 74 75 </section> 76 77 <!-- /.usage --> 78 79 <section class="notes"> 80 81 ## Notes 82 83 - Name order is not guaranteed, as `object` key enumeration is not specified according to the [ECMAScript specification][ecma-262-for-in]. In practice, however, most engines use insertion order to sort an `object`'s keys, thus allowing for deterministic extraction. 84 85 </section> 86 87 <!-- /.notes --> 88 89 <section class="examples"> 90 91 ## Examples 92 93 <!-- eslint no-undef: "error" --> 94 95 ```javascript 96 var defineProperty = require( '@stdlib/utils/define-property' ); 97 var inheritedKeys = require( '@stdlib/utils/inherited-keys' ); 98 99 function Foo() { 100 this.beep = 'boop'; 101 this.a = { 102 'b': 'c' 103 }; 104 defineProperty( this, 'baz', { 105 'configurable': false, 106 'enumerable': false, 107 'writable': true, 108 'value': 'qux' 109 }); 110 return this; 111 } 112 113 Foo.prototype.foo = [ 'bar' ]; 114 defineProperty( Foo.prototype, 'bip', { 115 'configurable': false, 116 'enumerable': false, 117 'writable': false, 118 'value': 'bop' 119 }); 120 121 var obj = new Foo(); 122 var keys = inheritedKeys( obj ); 123 124 console.log( keys ); 125 // => [ 'foo' ] 126 ``` 127 128 </section> 129 130 <!-- /.examples --> 131 132 <section class="links"> 133 134 [ecma-262-for-in]: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4 135 136 </section> 137 138 <!-- /.links -->