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