新功能范围

扫码查看
本文介绍了新功能范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段带有 ReferenceError 的代码 here

I've got a piece of code with a ReferenceError here

或以下:

function foo() {

    function bar() {
        console.log('bar');
    }

    var x = new Function('bar();');
    x();
}

foo(); // ReferenceError: bar is not defined

有可能实现吗?我的意思是存在于 new Function

Is it possible to make this happen? I mean the bar function to exist inside the new Function

推荐答案

MDN

所以不,它不是在使用这种方法时.

So no, it isn't while using that approach.

不要使用函数构造函数.它效率低下,将字符串转换为代码是充满问题.

Don't use the function constructor. It is inefficient, and converting strings to code is fraught with problems.

function foo() {

    function bar() {
        alert('bar');
    }

    function x() {
      bar();
    }
    x();
}

foo();

这篇关于新功能范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:31
查看更多