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