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: docs/src/rules/no-new-func.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,11 @@ rule_type: suggestion
7
7
It's possible to create functions in JavaScript from strings at runtime using the `Function` constructor, such as:
8
8
9
9
```js
10
-
var x=newFunction("a", "b", "return a + b");
11
-
var x=Function("a", "b", "return a + b");
12
-
var x=Function.call(null, "a", "b", "return a + b");
13
-
var x=Function.apply(null, ["a", "b", "return a + b"]);
14
-
var x =Function.bind(null, "a", "b", "return a + b")();
10
+
consta=newFunction("a", "b", "return a + b");
11
+
constb=Function("a", "b", "return a + b");
12
+
constc=Function.call(null, "a", "b", "return a + b");
13
+
constd=Function.apply(null, ["a", "b", "return a + b"]);
14
+
constx=Function.bind(null, "a", "b", "return a + b")();
15
15
```
16
16
17
17
This is considered by many to be a bad practice due to the difficulty in debugging and reading these types of functions. In addition, Content-Security-Policy (CSP) directives may disallow the use of `eval()` and similar methods for creating code from strings.
@@ -27,12 +27,12 @@ Examples of **incorrect** code for this rule:
27
27
```js
28
28
/*eslint no-new-func: "error"*/
29
29
30
-
var x=newFunction("a", "b", "return a + b");
31
-
var x=Function("a", "b", "return a + b");
32
-
var x=Function.call(null, "a", "b", "return a + b");
33
-
var x=Function.apply(null, ["a", "b", "return a + b"]);
34
-
var x =Function.bind(null, "a", "b", "return a + b")();
35
-
var f=Function.bind(null, "a", "b", "return a + b"); // assuming that the result of Function.bind(...) will be eventually called.
30
+
consta=newFunction("a", "b", "return a + b");
31
+
constb=Function("a", "b", "return a + b");
32
+
constc=Function.call(null, "a", "b", "return a + b");
33
+
constd=Function.apply(null, ["a", "b", "return a + b"]);
34
+
constx=Function.bind(null, "a", "b", "return a + b")();
35
+
consty=Function.bind(null, "a", "b", "return a + b"); // assuming that the result of Function.bind(...) will be eventually called.
36
36
```
37
37
38
38
:::
@@ -44,7 +44,7 @@ Examples of **correct** code for this rule:
0 commit comments