new Function 不能传入 object?

这样报错了

const obj = { a: 1 };
new Function([obj], `return a`)();

这样也报错了...

const obj = `{a:1}`
const obj = `{ a: 1 }`;
console.log(obj);
console.log(new Function([obj], `return obj`)());
阅读 1.3k
1 个回答

看下 Function 的构造器,其中 new(...args: string[]) 明确说明了,参数应该是字符串。

interface FunctionConstructor {
    /**
     * Creates a new function.
     * @param args A list of arguments the function accepts.
     */
    new(...args: string[]): Function;
    (...args: string[]): Function;
    readonly prototype: Function;
}

看下图,你就明白了
image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题