Update article.md

This commit is contained in:
Ilya Kantor 2017-11-01 14:57:36 +03:00 committed by GitHub
parent 0d81f751b1
commit b2559f4d39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,20 +10,22 @@ There's one more way to create a function. It's rarely used, but sometimes there
The syntax for creating a function: The syntax for creating a function:
```js ```js
let func = new Function('a', 'b', 'return a + b'); let func = new Function ([arg1[, arg2[, ...argN]],] functionBody)
``` ```
All arguments of `new Function` are strings. Parameters go first, and the body is the last. In other words, function parameters (or, more precise, names for them) go first, and the body is the last. All arguments are strings.
For instance: That's easy to understand by example.
For instance, here's a function with two arguments:
```js run ```js run
let sum = new Function('arg1', 'arg2', 'return arg1 + arg2'); let sum = new Function('a', 'b', 'return a + b');
alert( sum(1, 2) ); // 3 alert( sum(1, 2) ); // 3
``` ```
If there are no arguments, then there will be only body: If there are no arguments, then there's only one single argument, function body:
```js run ```js run
let sayHi = new Function('alert("Hello")'); let sayHi = new Function('alert("Hello")');
@ -136,4 +138,4 @@ new Function('a,b', ' return a + b; '); // comma-separated
new Function('a , b', ' return a + b; '); // comma-separated with spaces new Function('a , b', ' return a + b; '); // comma-separated with spaces
``` ```
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. 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.