README.md (2877B)
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 # eval 22 23 > Alias for [`eval`][mdn-eval] global. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var evil = require( '@stdlib/utils/eval' ); 31 ``` 32 33 #### evil( str ) 34 35 Alias for [`eval`][mdn-eval] global. 36 37 ```javascript 38 var v = evil( '5*4*3*2*1' ); 39 // returns 120 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ## Notes 49 50 - A reference to [`eval`][mdn-eval] **is** treated differently by the compiler. For example, when evaluating code containing block-scoped declarations (e.g., `let`, `const`, `function`, `class`), the compiler may throw an `error` complaining that block-scoped declarations are **not** yet supported outside of `strict mode`. One possible workaround is to include `"use strict";` in the evaluated code, as done in the example below. 51 52 </section> 53 54 <!-- /.notes --> 55 56 <section class="examples"> 57 58 ## Examples 59 60 <!-- eslint no-undef: "error" --> 61 62 ```javascript 63 var evil = require( '@stdlib/utils/eval' ); 64 65 var ctors; 66 var fcn; 67 var i; 68 69 function compile( ctor ) { 70 var name; 71 var str; 72 73 name = ctor.match( /^(\w*)Array$/ )[ 1 ]; 74 name += 'DataArray'; 75 76 str = ''; 77 str += '(function create(){'; 78 str += '"use strict";'; 79 str += 'class '+name+' extends '+ctor+'{'; 80 str += 'constructor(x){'; 81 str += 'super(x);'; 82 str += '}'; 83 str += '}'; 84 str += 'return '+name+';'; 85 str += '})();'; 86 return str; 87 } 88 89 ctors = [ 90 'Int8Array', 91 'Uint8Array', 92 'Uint8ClampedArray', 93 'Int16Array', 94 'Uint16Array', 95 'Int32Array', 96 'Uint32Array', 97 'Float32Array', 98 'Float64Array', 99 'Array' 100 ]; 101 102 for ( i = 0; i < ctors.length; i++ ) { 103 fcn = evil( compile( ctors[i] ) ); 104 console.log( fcn.toString() ); 105 } 106 ``` 107 108 </section> 109 110 <!-- /.examples --> 111 112 * * * 113 114 <section class="cli"> 115 116 ## CLI 117 118 <section class="usage"> 119 120 ### Usage 121 122 ```text 123 Usage: js-eval [options] <code> 124 125 Options: 126 127 -h, --help Print this message. 128 -V, --version Print the package version. 129 ``` 130 131 </section> 132 133 <!-- /.usage --> 134 135 <section class="examples"> 136 137 ### Examples 138 139 ```bash 140 $ js-eval '5*4*3*2*1' 141 120 142 ``` 143 144 </section> 145 146 <!-- /.examples --> 147 148 </section> 149 150 <!-- /.cli --> 151 152 <section class="links"> 153 154 [mdn-eval]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval 155 156 </section> 157 158 <!-- /.links -->