README.md (3062B)
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 # Symbol 22 23 > [Symbol][mdn-symbol] factory. 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 Symbol = require( '@stdlib/symbol/ctor' ); 41 ``` 42 43 #### Symbol( \[description] ) 44 45 Returns a [`Symbol`][mdn-symbol] primitive. 46 47 <!-- run-disable --> 48 49 <!-- eslint-disable symbol-description --> 50 51 ```javascript 52 var s = Symbol(); 53 // returns <symbol> 54 ``` 55 56 To aid debugging, provide a [symbol][mdn-symbol] `description`. 57 58 <!-- run-disable --> 59 60 ```javascript 61 var s = Symbol( 'beep' ); 62 // returns <symbol> 63 ``` 64 65 </section> 66 67 <!-- /.usage --> 68 69 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 70 71 <section class="notes"> 72 73 ## Notes 74 75 - Unlike conventional constructors, the function does **not** support the `new` keyword. 76 - The function is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `undefined`. 77 78 </section> 79 80 <!-- /.notes --> 81 82 <!-- Package usage examples. --> 83 84 <section class="examples"> 85 86 ## Examples 87 88 <!-- eslint no-undef: "error" --> 89 90 ```javascript 91 var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); 92 var Symbol = require( '@stdlib/symbol/ctor' ); 93 94 var s; 95 96 if ( hasSymbolSupport() ) { 97 s = Symbol( 'beep' ); 98 99 // Print the value type: 100 console.log( typeof s ); 101 // => 'symbol' 102 103 // Serialize the symbol as a string: 104 console.log( s.toString() ); 105 // => 'Symbol(beep)' 106 107 // Test symbol equality: 108 console.log( s === Symbol( 'beep' ) ); 109 // => false 110 } else { 111 console.log( 'Environment does not support symbols.' ); 112 } 113 ``` 114 115 </section> 116 117 <!-- /.examples --> 118 119 <!-- 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. --> 120 121 <section class="references"> 122 123 </section> 124 125 <!-- /.references --> 126 127 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 128 129 <section class="links"> 130 131 [mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol 132 133 </section> 134 135 <!-- /.links -->