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
+21-35Lines changed: 21 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,28 +8,28 @@ There's one more way to create a function. It's rarely used, but sometimes there
8
8
The syntax for creating a function:
9
9
10
10
```js
11
-
let func =newFunction ([arg1[, arg2[, ...argN]],] functionBody)
11
+
let func =newFunction ([arg1, arg2, ...argN], functionBody);
12
12
```
13
13
14
-
In other words, function parameters (or, more precisely, names for them) go first, and the body is last. All arguments are strings.
14
+
The function is created with the arguments `arg1...argN`and the given `functionBody`.
15
15
16
16
It's easier to understand by looking at an example. Here's a function with two arguments:
17
17
18
18
```js run
19
-
let sum =newFunction('a', 'b', 'return a + b');
19
+
let sum =newFunction('a', 'b', 'return a + b');
20
20
21
21
alert( sum(1, 2) ); // 3
22
22
```
23
23
24
-
If there are no arguments, then there's only a single argument, the function body:
24
+
And here there's a function without arguments, with only the function body:
25
25
26
26
```js run
27
27
let sayHi =newFunction('alert("Hello")');
28
28
29
29
sayHi(); // Hello
30
30
```
31
31
32
-
The major difference from other ways we've seen is that the function is created literally from a string, that is passed at run time.
32
+
The major difference from other ways we've seen is that the function is created literally from a string, that is passed at run time.
33
33
34
34
All previous declarations required us, programmers, to write the function code in the script.
35
35
@@ -42,16 +42,17 @@ let func = new Function(str);
42
42
func();
43
43
```
44
44
45
-
It is used in very specific cases, like when we receive code from a server, or to dynamically compile a function from a template. The need for that usually arises at advanced stages of development.
45
+
It is used in very specific cases, like when we receive code from a server, or to dynamically compile a function from a template, in complex web-applications.
46
46
47
47
## Closure
48
48
49
-
Usually, a function remembers where it was born in the special property `[[Environment]]`. It references the Lexical Environment from where it's created.
49
+
Usually, a function remembers where it was born in the special property `[[Environment]]`. It references the Lexical Environment from where it's created (we covered that in the chapter <info:closure>).
50
50
51
-
But when a function is created using `new Function`, its `[[Environment]]`references not the current Lexical Environment, but instead the global one.
51
+
But when a function is created using `new Function`, its `[[Environment]]`is set to reference not the current Lexical Environment, but the global one.
52
52
53
-
```js run
53
+
So, such function doesn't have access to outer variables, only to the global ones.
54
54
55
+
```js run
55
56
functiongetFunc() {
56
57
let value ="test";
57
58
@@ -67,7 +68,7 @@ getFunc()(); // error: value is not defined
67
68
68
69
Compare it with the regular behavior:
69
70
70
-
```js run
71
+
```js run
71
72
functiongetFunc() {
72
73
let value ="test";
73
74
@@ -87,51 +88,36 @@ Imagine that we must create a function from a string. The code of that function
87
88
88
89
Our new function needs to interact with the main script.
89
90
90
-
Perhaps we want it to be able to access outer local variables?
91
+
What if it could access the outer variables?
91
92
92
93
The problem is that before JavaScript is published to production, it's compressed using a *minifier* -- a special program that shrinks code by removing extra comments, spaces and -- what's important, renames local variables into shorter ones.
93
94
94
95
For instance, if a function has `let userName`, minifier replaces it with `let a` (or another letter if this one is occupied), and does it everywhere. That's usually a safe thing to do, because the variable is local, nothing outside the function can access it. And inside the function, minifier replaces every mention of it. Minifiers are smart, they analyze the code structure, so they don't break anything. They're not just a dumb find-and-replace.
95
96
96
-
But, if `new Function`could access outer variables, then it would be unable to find `userName`, since this is passed in as a string *after* the code is minified.
97
+
So if `new Function`had access to outer variables, it would be unable to find renamed `userName`.
97
98
98
-
**Even if we could access outer lexical environment in `new Function`, we would have problems with minifiers.**
99
+
**If `new Function` had access to outer variables, it would have problems with minifiers.**
99
100
100
-
The "special feature" of `new Function` saves us from mistakes.
101
+
Besides, such code would be architecturally bad and prone to errors.
101
102
102
-
And it enforces better code. If we need to pass something to a function created by `new Function`, we should pass it explicitly as an argument.
103
-
104
-
Our "sum" function actually does that right:
105
-
106
-
```js run
107
-
*!*
108
-
let sum =newFunction('a', 'b', 'return a + b');
109
-
*/!*
110
-
111
-
let a =1, b =2;
112
-
113
-
*!*
114
-
// outer values are passed as arguments
115
-
alert( sum(a, b) ); // 3
116
-
*/!*
117
-
```
103
+
To pass something to a function, created as `new Function`, we should use its arguments.
118
104
119
105
## Summary
120
106
121
107
The syntax:
122
108
123
109
```js
124
-
let func =newFunction(arg1, arg2, ..., body);
110
+
let func =newFunction ([arg1, arg2, ...argN], functionBody);
125
111
```
126
112
127
-
For historical reasons, arguments can also be given as a comma-separated list.
113
+
For historical reasons, arguments can also be given as a comma-separated list.
128
114
129
-
These three mean the same:
115
+
These three declarations mean the same:
130
116
131
-
```js
117
+
```js
132
118
newFunction('a', 'b', 'return a + b'); // basic syntax
133
119
newFunction('a,b', 'return a + b'); // comma-separated
134
120
newFunction('a , b', 'return a + b'); // comma-separated with spaces
135
121
```
136
122
137
-
Functions created with `new Function`, have `[[Environment]]` referencing the global Lexical Environment, not the outer one. Hence, they cannot use outer variables. But that's actually good, because it saves us from errors. Passing parameters explicitly is a much better method architecturally and causes no problems with minifiers.
123
+
Functions created with `new Function`, have `[[Environment]]` referencing the global Lexical Environment, not the outer one. Hence, they cannot use outer variables. But that's actually good, because it insures us from errors. Passing parameters explicitly is a much better method architecturally and causes no problems with minifiers.
0 commit comments