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