README.md (2927B)
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 # inheritedPropertyDescriptor 22 23 > Return a property descriptor for an object's inherited property. 24 25 <section class="usage"> 26 27 ## Usage 28 29 <!-- eslint-disable id-length --> 30 31 ```javascript 32 var inheritedPropertyDescriptor = require( '@stdlib/utils/inherited-property-descriptor' ); 33 ``` 34 35 #### inheritedPropertyDescriptor( obj, property\[, level] ) 36 37 Returns a property descriptor for an object's inherited property. 38 39 ```javascript 40 function Foo() { 41 return this; 42 } 43 44 Foo.prototype.bar = 'foo'; 45 46 var obj = new Foo(); 47 48 var desc = inheritedPropertyDescriptor( obj, 'bar' ); 49 // returns {'configurable':true,'enumerable':true,'writable':true,'value':'foo'} 50 ``` 51 52 By default, the function walks an object's entire prototype chain. To limit the inheritance level, provide a `level` argument. 53 54 ```javascript 55 var inherit = require( '@stdlib/utils/inherit' ); 56 57 function Bar() { 58 return this; 59 } 60 61 Bar.prototype.beep = 'boop'; 62 63 function Foo() { 64 Bar.call( this ); 65 return this; 66 } 67 68 inherit( Foo, Bar ); 69 70 var f = new Foo(); 71 var desc = inheritedPropertyDescriptor( f, 'beep', 1 ); 72 // returns null 73 ``` 74 75 </section> 76 77 <!-- /.usage --> 78 79 <section class="notes"> 80 81 ## Notes 82 83 - This function differs from the built-in `Object.getOwnPropertyDescriptor()` as follows: 84 85 - If provided `null` or `undefined`, the function returns `null`, rather than throwing an error. 86 - If an object does not have a provided inherited property, the function returns `null`, rather than `undefined`. 87 88 </section> 89 90 <!-- /.notes --> 91 92 <section class="examples"> 93 94 ## Examples 95 96 <!-- eslint-disable id-length --> 97 98 <!-- eslint no-undef: "error" --> 99 100 ```javascript 101 var defineProperty = require( '@stdlib/utils/define-property' ); 102 var inheritedPropertyDescriptor = require( '@stdlib/utils/inherited-property-descriptor' ); 103 104 function Foo() { 105 this.beep = 'boop'; 106 this.a = { 107 'b': 'c' 108 }; 109 defineProperty( this, 'baz', { 110 'value': 'qux', 111 'configurable': true, 112 'writable': true, 113 'enumerable': false 114 }); 115 return this; 116 } 117 118 Foo.prototype.foo = [ 'bar' ]; 119 120 var obj = new Foo(); 121 var desc = inheritedPropertyDescriptor( obj, 'foo' ); 122 123 console.log( desc ); 124 // => {'configurable':true,'enumerable':true,'writable':true,'value':['bar']} 125 ``` 126 127 </section> 128 129 <!-- /.examples --> 130 131 <section class="links"> 132 133 </section> 134 135 <!-- /.links -->