README.md (2482B)
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 Memoized Read-Only 22 23 > [Define][mdn-define-property] a configurable memoized **read-only** object property. 24 25 <section class="usage"> 26 27 ## Usage 28 29 <!-- eslint-disable id-length --> 30 31 ```javascript 32 var setMemoizedConfigurableReadOnly = require( '@stdlib/utils/define-memoized-configurable-read-only-property' ); 33 ``` 34 35 #### setMemoizedConfigurableReadOnly( obj, prop, fcn ) 36 37 [Defines][mdn-define-property] a configurable memoized **read-only** object property. 38 39 <!-- eslint-disable id-length --> 40 41 ```javascript 42 var obj = {}; 43 44 function foo() { 45 return 'bar'; 46 } 47 48 setMemoizedConfigurableReadOnly( obj, 'foo', foo ); 49 50 var v = obj.foo; 51 // returns 'bar' 52 ``` 53 54 The last argument should be a synchronous function whose return value will be memoized and set as the property value. 55 56 </section> 57 58 <!-- /.usage --> 59 60 <section class="notes"> 61 62 ## Notes 63 64 - A configurable **read-only** property is **enumerable**. 65 66 </section> 67 68 <!-- /.notes --> 69 70 <section class="examples"> 71 72 ## Examples 73 74 <!-- eslint no-undef: "error" --> 75 76 <!-- eslint-disable id-length --> 77 78 ```javascript 79 var fibonacci = require( '@stdlib/math/base/special/fibonacci' ); 80 var setMemoizedConfigurableReadOnly = require( '@stdlib/utils/define-memoized-configurable-read-only-property' ); 81 82 function Foo() { 83 var self; 84 if ( !(this instanceof Foo) ) { 85 return new Foo(); 86 } 87 self = this; 88 this.count = 0; 89 setMemoizedConfigurableReadOnly( this, 'fibo', fibo ); 90 return this; 91 92 function fibo() { 93 self.count += 1; 94 return fibonacci( 73 ); 95 } 96 } 97 98 var foo = new Foo(); 99 100 var i; 101 for ( i = 0; i < 10; i++ ) { 102 console.log( 'F: %d. Count: %d.', foo.fibo, foo.count ); 103 } 104 ``` 105 106 </section> 107 108 <!-- /.examples --> 109 110 <section class="links"> 111 112 [mdn-define-property]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty 113 114 </section> 115 116 <!-- /.links -->