You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/07-new-function/article.md
+8-6Lines changed: 8 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -10,20 +10,22 @@ There's one more way to create a function. It's rarely used, but sometimes there
10
10
The syntax for creating a function:
11
11
12
12
```js
13
-
let func =newFunction('a', 'b', 'return a + b');
13
+
let func =newFunction ([arg1[, arg2[, ...argN]],] functionBody)
14
14
```
15
15
16
-
All arguments of `new Function` are strings. Parameters go first, and the body is the last.
16
+
In other words, function parameters (or, more precise, names for them) go first, and the body is the last. All arguments are strings.
17
17
18
-
For instance:
18
+
That's easy to understand by example.
19
+
20
+
For instance, here's a function with two arguments:
19
21
20
22
```js run
21
-
let sum =newFunction('arg1', 'arg2', 'return arg1 + arg2');
23
+
let sum =newFunction('a', 'b', 'return a + b');
22
24
23
25
alert( sum(1, 2) ); // 3
24
26
```
25
27
26
-
If there are no arguments, then there will be only body:
28
+
If there are no arguments, then there's only one single argument, function body:
27
29
28
30
```js run
29
31
let sayHi =newFunction('alert("Hello")');
@@ -136,4 +138,4 @@ new Function('a,b', ' return a + b; '); // comma-separated
136
138
newFunction('a , b', ' return a + b; '); // comma-separated with spaces
137
139
```
138
140
139
-
Functions created with `new Function`, have `[[Environment]]` referencing the global Lexical Environment, not the outer one. Hence, they can not use outer variables. But that's actually good, because it saves us from errors. Explicit parameters passing is a much better thing architecturally and has no problems with minifiers.
141
+
Functions created with `new Function`, have `[[Environment]]` referencing the global Lexical Environment, not the outer one. Hence, they can not use outer variables. But that's actually good, because it saves us from errors. Explicit parameters passing is a much better thing architecturally and has no problems with minifiers.
0 commit comments