|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2022 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 | +# Function |
| 22 | + |
| 23 | +> [Function][mdn-function] constructor. |
| 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 | +<!-- eslint-disable stdlib/no-redeclare --> |
| 40 | + |
| 41 | +```javascript |
| 42 | +var Function = require( '@stdlib/function/ctor' ); |
| 43 | +``` |
| 44 | + |
| 45 | +#### Function( \[...argNames,] body ) |
| 46 | + |
| 47 | +Returns a new [function][mdn-function] object. |
| 48 | + |
| 49 | +<!-- eslint-disable stdlib/no-redeclare --> |
| 50 | + |
| 51 | +```javascript |
| 52 | +var greet = new Function( 'name', 'return "Hello, "+name+"!"' ); |
| 53 | + |
| 54 | +var v = greet( 'Jane' ); |
| 55 | +// returns 'Hello, Jane!' |
| 56 | +``` |
| 57 | + |
| 58 | +Argument names must be strings corresponding to valid JavaScript parameters (i.e., a plain identifier, or, in environments supporting such parameters, a rest parameter or destructured parameter, optionally with a default). |
| 59 | + |
| 60 | +</section> |
| 61 | + |
| 62 | +<!-- /.usage --> |
| 63 | + |
| 64 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 65 | + |
| 66 | +<section class="notes"> |
| 67 | + |
| 68 | +## Notes |
| 69 | + |
| 70 | +- In pre-ES2015 environments, only plain identifiers (without defaults) are valid JavaScript parameters. |
| 71 | +- Creating `Function` objects with the `Function` constructor is less efficient than declaring a function via a function expression or a function statement. |
| 72 | +- The `Function` constructor can be invoked without the `new` operator (using `new` and not using `new` both return a new `Function` object). |
| 73 | +- The `Function` constructor creates functions which execute in the **global scope**. Hence, created functions **cannot** access variables local to the scope in which functions were created. |
| 74 | + |
| 75 | +</section> |
| 76 | + |
| 77 | +<!-- /.notes --> |
| 78 | + |
| 79 | +<!-- Package usage examples. --> |
| 80 | + |
| 81 | +<section class="examples"> |
| 82 | + |
| 83 | +## Examples |
| 84 | + |
| 85 | +<!-- eslint no-undef: "error" --> |
| 86 | + |
| 87 | +<!-- eslint-disable stdlib/no-redeclare --> |
| 88 | + |
| 89 | +```javascript |
| 90 | +var Function = require( '@stdlib/function/ctor' ); |
| 91 | + |
| 92 | +var add = new Function( 'x', 'y', 'return x + y' ); |
| 93 | + |
| 94 | +var v = add( 1, 2 ); |
| 95 | +// returns 3 |
| 96 | +``` |
| 97 | + |
| 98 | +</section> |
| 99 | + |
| 100 | +<!-- /.examples --> |
| 101 | + |
| 102 | +<!-- 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. --> |
| 103 | + |
| 104 | +<section class="references"> |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.references --> |
| 109 | + |
| 110 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 111 | + |
| 112 | +<section class="related"> |
| 113 | + |
| 114 | +</section> |
| 115 | + |
| 116 | +<!-- /.related --> |
| 117 | + |
| 118 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 119 | + |
| 120 | +<section class="links"> |
| 121 | + |
| 122 | +[mdn-function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.links --> |
0 commit comments