Creating Functions Using new Function
Syntax new Function ([arg1[, arg2[, …argN]],] functionBody)
Creating a function by using the constructor to generate a new instance has the following characteristics compared with function declarations:
- Functions can be created dynamically
- All functions are anonymous functions, and Function.name is always anonymous
- Local variables cannot be referenced inside the function; only global variables can be referenced
var x = 10;
function createFunction1() {
var x = 20;
return new Function("return x;"); // this |x| refers global |x|
}
function createFunction2() {
var x = 20;
function f() {
return x; // this |x| refers local |x| above
}
return f;
}
var f1 = createFunction1();
console.log(f1()); // 10
var f2 = createFunction2();
console.log(f2()); // 20热门文章
-
Using PVE to Run a Clash Bypass Router Virtual Machine for Transparent Proxying
-
Page Load Performance Optimization Solutions
-
Nine Years of Bookkeeping: Changes in Tools, Methods, and Mindset
-
My Experience Unbricking a Netgear R6300V2 Router
-
Prismira: Two and a Half Years of Iteration and Growth
-
Hand-sized Mini PC Yunxuan H170 Usage Experience and Homelab Setup

