细说 new Function

使用方式

javascript 复制代码
//多参数形式
const add1 = new Function('a', 'b', 'return a + b');

// 参数合并形式
const add2 = new Function('a, b', 'return a + b');

// add1 与 add2 等价以下 add 函数,区别参考下文「特点」
const add = (a, b) => a + b;

new Function 创建的函数的特点

1. 在全局作用域中执行

javascript 复制代码
const x = 10;

function createFunction() {
    const x = 20;
    // 这个函数只能访问全局作用域,不能访问 createFunction 的局部作用域
    return new Function('return x');
}

const func = createFunction();
console.log(func()); // 10

2. 运行时编译

javascript 复制代码
// 可以从字符串动态创建函数
const operation = 'multiply';
const funcBody = operation === 'add' 
    ? 'return a + b' 
    : 'return a * b';

const func = new Function('a', 'b', funcBody);
console.log(func(5, 3)); // 15

3. 无法访问外部变量(无闭包)

javascript 复制代码
function outer() {
    const secret = 'hidden';
    
    // 传统函数定义可以访问外部变量
    const normalFunc = function() {
        return secret;
    };
    
    // new Function 不能访问外部变量
    const newFunc = new Function('return secret');
    
    console.log(normalFunc()); // 'hidden'
    console.log(newFunc()); // ReferenceError: secret is not defined
}

Function 中的 this

默认情况指向全局对象

默认情况下,new Function 创建的函数中 this 指向全局对象,即浏览器中是 window,Node.js 中是 global。这也是 Function 的特点,即在全局作用域中执行。

javascript 复制代码
// 在浏览器中
const func = new Function('return this');
console.log(func()); // Window 对象(浏览器环境)

// 在 Node.js 中
const func = new Function('return this');
console.log(func() === global); // true(Node.js 环境)

严格模式是 undefined

javascript 复制代码
// 使用严格模式
const func = new Function('"use strict"; return this');
console.log(func()); // undefined

不受调用上下文影响

不会像普通函数那样根据调用方式改变 this

javascript 复制代码
const obj = {
    value: 'object value',
    
    // 普通函数
    normalMethod: function() {
        return this.value;
    },
    
    // new Function 创建的函数
    newFunctionMethod: new Function('return this.value')
};

console.log(obj.normalMethod());  // 'object value'
console.log(obj.newFunctionMethod());  // undefined

需要显式绑定

如果想要在 Function 内使用 this,可以通过 callapplybind 来指定 this

javascript 复制代码
const obj = { value: 'test' };
const func = new Function('return this.value');

console.log(func.call(obj)); // 'test'
console.log(func.apply(obj)); // 'test'

const boundFunc = func.bind(obj);
console.log(boundFunc()); // 'test'
相关推荐
她是太阳,好耀眼i5 小时前
Nvm 实现vue版本切换
javascript·vue.js·ecmascript
蒲公英10015 小时前
在wps软件的word中使用js宏命令设置表格背景色
javascript·word·wps
一枚前端小能手5 小时前
📜 `<script>`脚本元素 - 从加载策略到安全性与性能的完整指南
前端·javascript
掘金安东尼5 小时前
TypeScript为何在AI时代登顶:Anders Hejlsberg 的十二年演化论
前端·javascript·面试
执携6 小时前
Vue Router (命名视图)
前端·javascript·vue.js
含若飞7 小时前
Vue 中 `watch` 与 `this.$watch` 使用指南
前端·javascript·vue.js
Python私教7 小时前
Node.js 开发环境搭建全攻略(2025版)
javascript
希冀1238 小时前
【Vue】第五篇
前端·javascript·vue.js
www_stdio8 小时前
JavaScript 中的数组:开箱即用却暗藏玄机
javascript
顾安r9 小时前
11.10 脚本算法 五子棋 「重要」
服务器·前端·javascript·游戏·flask