README.md (3203B)
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 # Iterator Symbol 22 23 > Iterator [symbol][mdn-symbol] which specifies the default iterator for an object. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var IteratorSymbol = require( '@stdlib/symbol/iterator' ); 41 ``` 42 43 #### IteratorSymbol 44 45 Iterator [`symbol`][mdn-symbol] which specifies the default iterator for an object. 46 47 ```javascript 48 var s = typeof IteratorSymbol; 49 // e.g., returns 'symbol' 50 ``` 51 52 </section> 53 54 <!-- /.usage --> 55 56 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 57 58 <section class="notes"> 59 60 ## Notes 61 62 - The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`. 63 64 </section> 65 66 <!-- /.notes --> 67 68 <!-- Package usage examples. --> 69 70 <section class="examples"> 71 72 ## Examples 73 74 <!-- eslint no-undef: "error" --> 75 76 ```javascript 77 var IteratorSymbol = require( '@stdlib/symbol/iterator' ); 78 79 var obj; 80 var v; 81 82 function iterator() { 83 var iter; 84 var i; 85 86 i = -1; 87 88 iter = {}; 89 iter.next = next; 90 iter.return = done; 91 92 if ( IteratorSymbol ) { 93 // Allow the iterator to work with `for...of`: 94 iter[ IteratorSymbol ] = iterator; 95 } 96 return iter; 97 98 function next() { 99 i += 1; 100 return { 101 'value': i, 102 'done': false 103 }; 104 } 105 106 function done( value ) { 107 if ( arguments.length === 0 ) { 108 return { 109 'done': true 110 }; 111 } 112 return { 113 'value': value, 114 'done': true 115 }; 116 } 117 } 118 119 obj = iterator(); 120 while ( v === void 0 || ( v.value < 10 && v.done === false ) ) { 121 v = obj.next(); 122 console.log( v.value ); 123 } 124 ``` 125 126 </section> 127 128 <!-- /.examples --> 129 130 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 131 132 <section class="references"> 133 134 </section> 135 136 <!-- /.references --> 137 138 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 139 140 <section class="links"> 141 142 [mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol 143 144 </section> 145 146 <!-- /.links -->