README.md (2388B)
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-Write Accessor 22 23 > [Define][@stdlib/utils/define-property] a configurable **read-write** accessor. 24 25 <section class="usage"> 26 27 ## Usage 28 29 <!-- eslint-disable id-length --> 30 31 ```javascript 32 var setConfigurableReadWriteAccessor = require( '@stdlib/utils/define-configurable-read-write-accessor' ); 33 ``` 34 35 #### setConfigurableReadWriteAccessor( obj, prop, getter, setter ) 36 37 [Defines][@stdlib/utils/define-property] a configurable **read-write** accessor. 38 39 <!-- eslint-disable id-length --> 40 41 ```javascript 42 var word = 'bar'; 43 var obj = {}; 44 45 function getter() { 46 return word + ' foo'; 47 } 48 49 function setter( v ) { 50 word = v; 51 } 52 53 setConfigurableReadWriteAccessor( obj, 'foo', getter, setter ); 54 55 var v = obj.foo; 56 // returns 'bar foo' 57 58 obj.foo = 'beep'; 59 60 v = obj.foo; 61 // returns 'beep foo' 62 ``` 63 64 </section> 65 66 <!-- /.usage --> 67 68 <section class="notes"> 69 70 ## Notes 71 72 - Configurable read-write accessors are **enumerable**. 73 74 </section> 75 76 <!-- /.notes --> 77 78 <section class="examples"> 79 80 ## Examples 81 82 <!-- eslint-disable id-length --> 83 84 <!-- eslint no-undef: "error" --> 85 86 ```javascript 87 var setConfigurableReadWriteAccessor = require( '@stdlib/utils/define-configurable-read-write-accessor' ); 88 89 function Foo( name ) { 90 if ( !(this instanceof Foo) ) { 91 return new Foo( name ); 92 } 93 setConfigurableReadWriteAccessor( this, 'name', getName, setName ); 94 return this; 95 96 function getName() { 97 return 'Hello, ' + name; 98 } 99 100 function setName( v ) { 101 name = v; 102 } 103 } 104 105 var foo = new Foo( 'Grace' ); 106 console.log( foo.name ); 107 108 foo.name = 'Ada'; 109 console.log( foo.name ); 110 ``` 111 112 </section> 113 114 <!-- /.examples --> 115 116 <section class="links"> 117 118 [@stdlib/utils/define-property]: https://www.npmjs.com/package/@stdlib/utils/tree/main/define-property 119 120 </section> 121 122 <!-- /.links -->