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