104 lines
1.9 KiB
Markdown
104 lines
1.9 KiB
Markdown
|
<!--
|
||
|
|
||
|
@license Apache-2.0
|
||
|
|
||
|
Copyright (c) 2018 The Stdlib Authors.
|
||
|
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
|
||
|
-->
|
||
|
|
||
|
# Write-Only Accessor
|
||
|
|
||
|
> [Define][@stdlib/utils/define-property] a **write-only** accessor.
|
||
|
|
||
|
<section class="usage">
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
```javascript
|
||
|
var setWriteOnlyAccessor = require( '@stdlib/utils/define-write-only-accessor' );
|
||
|
```
|
||
|
|
||
|
#### setWriteOnlyAccessor( obj, prop, setter )
|
||
|
|
||
|
[Defines][@stdlib/utils/define-property] a **write-only** accessor.
|
||
|
|
||
|
```javascript
|
||
|
var obj = {};
|
||
|
var val = '';
|
||
|
|
||
|
function setter( v ) {
|
||
|
val = v;
|
||
|
}
|
||
|
|
||
|
setWriteOnlyAccessor( obj, 'foo', setter );
|
||
|
|
||
|
obj.foo = 'boop';
|
||
|
|
||
|
var bool = ( val === 'boop' );
|
||
|
// returns true
|
||
|
```
|
||
|
|
||
|
</section>
|
||
|
|
||
|
<!-- /.usage -->
|
||
|
|
||
|
<section class="notes">
|
||
|
|
||
|
## Notes
|
||
|
|
||
|
- Write-only accessors are **enumerable** and **non-configurable**.
|
||
|
|
||
|
</section>
|
||
|
|
||
|
<!-- /.notes -->
|
||
|
|
||
|
<section class="examples">
|
||
|
|
||
|
## Examples
|
||
|
|
||
|
<!-- eslint no-undef: "error" -->
|
||
|
|
||
|
```javascript
|
||
|
var setWriteOnlyAccessor = require( '@stdlib/utils/define-write-only-accessor' );
|
||
|
|
||
|
function Foo( secret ) {
|
||
|
if ( !(this instanceof Foo) ) {
|
||
|
return new Foo( secret );
|
||
|
}
|
||
|
setWriteOnlyAccessor( this, 'secret', setter );
|
||
|
return this;
|
||
|
|
||
|
function setter( v ) {
|
||
|
secret = v;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var foo = new Foo( 'beep' );
|
||
|
|
||
|
foo.secret = 'boop';
|
||
|
```
|
||
|
|
||
|
</section>
|
||
|
|
||
|
<!-- /.examples -->
|
||
|
|
||
|
<section class="links">
|
||
|
|
||
|
[@stdlib/utils/define-property]: https://www.npmjs.com/package/@stdlib/utils/tree/main/define-property
|
||
|
|
||
|
</section>
|
||
|
|
||
|
<!-- /.links -->
|