Skip to content

Commit ee565f8

Browse files
committed
Add Function constructor
1 parent af85fef commit ee565f8

10 files changed

Lines changed: 539 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 -->
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pkg = require( './../package.json' ).name;
25+
var Fcn = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+'::instantiation', function benchmark( b ) {
31+
var f;
32+
var i;
33+
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = new Fcn( 'x', 'y', 'return x + y' );
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::evaluation', function benchmark( b ) {
50+
var f;
51+
var v;
52+
var i;
53+
54+
f = new Fcn( 'x', 'y', 'return x + y' );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = f( i, i+1 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( pkg+'::inline', function benchmark( b ) {
72+
var v;
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
v = add( i, i+1 );
78+
if ( v !== v ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
}
82+
b.toc();
83+
if ( v !== v ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
89+
function add( x, y ) {
90+
return x + y;
91+
}
92+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( [...argNames,] body )
3+
Returns a Function object.
4+
5+
Argument names must be strings corresponding to valid JavaScript parameters
6+
(i.e., a plain identifier, or, in environments supporting such parameters, a
7+
rest parameter or destructured parameter, optionally with a default).
8+
9+
Parameters
10+
----------
11+
argNames: ...any (optional)
12+
Parameter names.
13+
14+
body: string
15+
Function body.
16+
17+
Returns
18+
-------
19+
fcn: Function
20+
Function object.
21+
22+
Examples
23+
--------
24+
> var f = new {{alias}}( 'x', 'y', 'return x + y' );
25+
> f( 1, 2 )
26+
3
27+
28+
See Also
29+
--------
30+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
// EXPORTS //
22+
23+
/**
24+
* Returns a Function object.
25+
*
26+
* ## Notes
27+
*
28+
* - 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).
29+
* - Creating `Function` objects with the `Function` constructor is less efficient than declaring a function via a function expression or a function statement.
30+
* - The `Function` constructor can be invoked without the `new` operator (using `new` and not using `new` both return a new `Function` object).
31+
* - 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.
32+
*
33+
* @param argNames - parameter names
34+
* @param body - function body
35+
* @returns function
36+
*
37+
* @example
38+
* var add = new Function( 'x', 'y', 'return x + y' );
39+
*
40+
* var v = add( 1, 2 );
41+
* // returns 3
42+
*/
43+
export = Function;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// tslint:disable: no-construct
20+
// tslint:disable: no-unused-expression
21+
22+
import Fcn = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a function object...
28+
{
29+
new Fcn( 'x', 'y', 'return x + y' ); // $ExpectType Function
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var Function = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare
22+
23+
var add = new Function( 'x', 'y', 'return x + y' );
24+
25+
var v = add( 1, 2 );
26+
// returns 3
27+
28+
console.log( v );
29+
// => 3
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Function constructor.
23+
*
24+
* @module @stdlib/function/ctor
25+
*
26+
* @example
27+
* var Function = require( '@stdlib/function/ctor' );
28+
*
29+
* var add = new Function( 'x', 'y', 'return x + y' );
30+
*
31+
* var v = add( 1, 2 );
32+
* // returns 3
33+
*/
34+
35+
// MODULES //
36+
37+
var main = require( './main.js' );
38+
39+
40+
// EXPORTS //
41+
42+
module.exports = main;

0 commit comments

Comments
 (0)