|
| 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 | +# inheritedEnumerableProperties |
| 22 | + |
| 23 | +> Return an array of an object's inherited enumerable property names and [symbols][@stdlib/symbol/ctor]. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +<!-- eslint-disable id-length --> |
| 30 | + |
| 31 | +```javascript |
| 32 | +var inheritedEnumerableProperties = require( '@stdlib/utils/inherited-enumerable-properties' ); |
| 33 | +``` |
| 34 | + |
| 35 | +#### inheritedEnumerableProperties( obj\[, level] ) |
| 36 | + |
| 37 | +Returns an `array` of an object's inherited enumerable property names and [symbols][@stdlib/symbol/ctor]. |
| 38 | + |
| 39 | +```javascript |
| 40 | +var defineProperty = require( '@stdlib/utils/define-property' ); |
| 41 | + |
| 42 | +function Foo() { |
| 43 | + this.a = 'b'; |
| 44 | + return this; |
| 45 | +} |
| 46 | + |
| 47 | +Foo.prototype.beep = 'boop'; |
| 48 | + |
| 49 | +var f = new Foo(); |
| 50 | +var props = inheritedEnumerableProperties( f ); |
| 51 | +// returns [ 'beep' ] |
| 52 | +``` |
| 53 | + |
| 54 | +By default, the function walks an object's entire prototype chain. To limit the inheritance level, provide a `level` argument. |
| 55 | + |
| 56 | +```javascript |
| 57 | +var defineProperty = require( '@stdlib/utils/define-property' ); |
| 58 | +var inherit = require( '@stdlib/utils/inherit' ); |
| 59 | + |
| 60 | +function Bar() { |
| 61 | + return this; |
| 62 | +} |
| 63 | + |
| 64 | +Bar.prototype.boop = 'beep'; |
| 65 | + |
| 66 | +function Foo() { |
| 67 | + Bar.call( this ); |
| 68 | + this.a = 'b'; |
| 69 | + return this; |
| 70 | +} |
| 71 | + |
| 72 | +inherit( Foo, Bar ); |
| 73 | +Foo.prototype.beep = 'boop'; |
| 74 | + |
| 75 | +var f = new Foo(); |
| 76 | +var pros = inheritedEnumerableProperties( f, 1 ); |
| 77 | +// returns [ 'beep' ] |
| 78 | +``` |
| 79 | + |
| 80 | +</section> |
| 81 | + |
| 82 | +<!-- /.usage --> |
| 83 | + |
| 84 | +<section class="notes"> |
| 85 | + |
| 86 | +## Notes |
| 87 | + |
| 88 | +- Property order is not guaranteed, as `object` property 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 properties, thus allowing for deterministic extraction. |
| 89 | + |
| 90 | +</section> |
| 91 | + |
| 92 | +<!-- /.notes --> |
| 93 | + |
| 94 | +<section class="examples"> |
| 95 | + |
| 96 | +## Examples |
| 97 | + |
| 98 | +<!-- eslint-disable id-length --> |
| 99 | + |
| 100 | +<!-- eslint no-undef: "error" --> |
| 101 | + |
| 102 | +```javascript |
| 103 | +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); |
| 104 | +var Symbol = require( '@stdlib/symbol/ctor' ); |
| 105 | +var defineProperty = require( '@stdlib/utils/define-property' ); |
| 106 | +var inheritedEnumerableProperties = require( '@stdlib/utils/inherited-enumerable-properties' ); |
| 107 | + |
| 108 | +var hasSymbols = hasSymbolSupport(); |
| 109 | +var props; |
| 110 | +var obj; |
| 111 | + |
| 112 | +function Foo() { |
| 113 | + this.beep = 'boop'; |
| 114 | + this.a = { |
| 115 | + 'b': 'c' |
| 116 | + }; |
| 117 | + defineProperty( this, 'baz', { |
| 118 | + 'configurable': false, |
| 119 | + 'enumerable': false, |
| 120 | + 'writable': true, |
| 121 | + 'value': 'qux' |
| 122 | + }); |
| 123 | + if ( hasSymbols ) { |
| 124 | + this[ Symbol( 'a' ) ] = 'b'; |
| 125 | + defineProperty( this, 'beep', { |
| 126 | + 'configurable': false, |
| 127 | + 'enumerable': false, |
| 128 | + 'writable': false, |
| 129 | + 'value': 'boop' |
| 130 | + }); |
| 131 | + } |
| 132 | + return this; |
| 133 | +} |
| 134 | + |
| 135 | +Foo.prototype.c = 'd'; |
| 136 | +defineProperty( Foo.prototype, 'bip', { |
| 137 | + 'configurable': false, |
| 138 | + 'enumerable': false, |
| 139 | + 'writable': false, |
| 140 | + 'value': 'bop' |
| 141 | +}); |
| 142 | +if ( hasSymbols ) { |
| 143 | + Foo.prototype[ Symbol( 'c' ) ] = 'd'; |
| 144 | + defineProperty( Foo.prototype, Symbol( 'e' ), { |
| 145 | + 'configurable': false, |
| 146 | + 'enumerable': false, |
| 147 | + 'writable': false, |
| 148 | + 'value': 'f' |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +obj = new Foo(); |
| 153 | +props = inheritedEnumerableProperties( obj ); |
| 154 | + |
| 155 | +console.log( props ); |
| 156 | +// e.g., => [ 'c', ... ] |
| 157 | +``` |
| 158 | + |
| 159 | +</section> |
| 160 | + |
| 161 | +<!-- /.examples --> |
| 162 | + |
| 163 | +<section class="links"> |
| 164 | + |
| 165 | +[ecma-262-for-in]: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4 |
| 166 | + |
| 167 | +[@stdlib/symbol/ctor]: https://github.com/stdlib-js/stdlib |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.links --> |
0 commit comments