README.md (2205B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2019 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 # Configurable Read-Only Accessor 22 23 > [Define][@stdlib/utils/define-property] a configurable **read-only** accessor. 24 25 <section class="usage"> 26 27 ## Usage 28 29 <!-- eslint-disable id-length --> 30 31 ```javascript 32 var setConfigurableReadOnlyAccessor = require( '@stdlib/utils/define-configurable-read-only-accessor' ); 33 ``` 34 35 #### setConfigurableReadOnlyAccessor( obj, prop, getter ) 36 37 [Defines][@stdlib/utils/define-property] a configurable **read-only** accessor. 38 39 <!-- run throws: true --> 40 41 <!-- eslint-disable id-length --> 42 43 ```javascript 44 function getter() { 45 return 'bar'; 46 } 47 48 var obj = {}; 49 50 setConfigurableReadOnlyAccessor( obj, 'foo', getter ); 51 52 obj.foo = 'boop'; 53 // throws <Error> 54 ``` 55 56 </section> 57 58 <!-- /.usage --> 59 60 <section class="notes"> 61 62 ## Notes 63 64 - Configurable read-only accessors are **enumerable**. 65 66 </section> 67 68 <!-- /.notes --> 69 70 <section class="examples"> 71 72 ## Examples 73 74 <!-- eslint-disable id-length --> 75 76 <!-- eslint no-undef: "error" --> 77 78 ```javascript 79 var setConfigurableReadOnlyAccessor = require( '@stdlib/utils/define-configurable-read-only-accessor' ); 80 81 function Foo( name ) { 82 if ( !(this instanceof Foo) ) { 83 return new Foo( name ); 84 } 85 setConfigurableReadOnlyAccessor( this, 'name', getName ); 86 return this; 87 88 function getName() { 89 return name; 90 } 91 } 92 93 var foo = new Foo( 'beep' ); 94 95 try { 96 foo.name = 'boop'; 97 } catch ( err ) { 98 console.error( err.message ); 99 } 100 ``` 101 102 </section> 103 104 <!-- /.examples --> 105 106 <section class="links"> 107 108 [@stdlib/utils/define-property]: https://www.npmjs.com/package/@stdlib/utils/tree/main/define-property 109 110 </section> 111 112 <!-- /.links -->