javascript 函数只能在严格模式的顶层声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33359840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:24:58  来源:igfitidea点击:

Functions may be declared only at top level in strict mode

javascript

提问by Sir

I have this error when using FireFox with strict mode. But am unsure what it means. I assumed it meant that the function had to be declared before it was called upon but the error still occurs.

在严格模式下使用 FireFox 时出现此错误。但我不确定这意味着什么。我认为这意味着必须在调用该函数之前声明该函数,但错误仍然发生。

SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function

SyntaxError:在严格模式代码中,函数只能在顶层声明或立即在另一个函数中声明

This is my snippet of code where it is causing the error:

这是我导致错误的代码片段:

var process = new function(){

  var self = this;

  self.test = function(value,callback){
    var startTime = Date.now();

     function update(){     //<--- error is here
                value++;
                startTime        = Date.now();

                if(value < 100){ 
                    setTimeout(update, 0);
                }
                callback(value);
    }       
    update();
  }

};

So i'm wondering how would I write this snippet of code out correctly with strict ? What does it mean by top level ? Does that mean globally defined and not locally within a function ?

所以我想知道如何使用 strict 正确写出这段代码?顶级是什么意思?这是否意味着全局定义而不是在函数内局部定义?

Also given I have use strictwhy does this problem not occur in Chrome?

还考虑到我use strict为什么在 Chrome 中不会出现这个问题?

回答by jfriend00

You must put local functions BEFORE other code within the parent function in strict mode:

您必须在严格模式下将本地函数放在父函数中的其他代码之前:

var process = function () {
    var self = this;
    self.test = function (value, callback) {

        function update() {
            value++;
            startTime = Date.now();
            if (value < 100) {
                setTimeout(update, 0);
            }
            callback(value);
        }

        var startTime = Date.now();
        update();
    }
};

This is described in this articles:

这在这篇文章中有描述:

New ES5 strict mode requirement: function statements not at top level of a program or function are prohibited

新的 ES5 严格模式要求:禁止不在程序或函数顶层的函数语句

MDN Strict Mode

MDN 严格模式

In my own testing though (and counter to the articles I've read), I find that current versions of both Chrome and Firefox only complain about a local function definition if it is inside a block (like inside an ifor forstatement or a similar block.

在我自己的测试中(并且与我读过的文章相反),我发现当前版本的 Chrome 和 Firefox 只会抱怨本地函数定义在块内(例如在iforfor语句或类似块内) .

I guess I need to go find an actual spec to see what is says.

我想我需要去找一个实际的规范来看看是什么意思。

回答by Y.K.

The Internet explorer error explicitly states functions names cannot be "declared" within a function. So using a self-invoking function expression has worked for me.

Internet Explorer 错误明确指出不能在函数内“声明”函数名称。所以使用自调用函数表达式对我有用。

This code fails:

此代码失败:

    c.prototype.isitReal=function(t){
    var matched = false;
    this.each(function(item){
        // declaring a new function within a function fails 
        function getChildren(el){
            ....
            getChildren(el[a].children);
            ....
        }
        getChildren(el.children); // invoke function
    });
    return matched;
};

This code works:

此代码有效:

    c.prototype.isitReal=function(t){
    var matched = false;
    this.each(function(item){
        // convert the function to an expression of a function
        // by wraping the entire function in ( )

        (function getChildren(el){
            ....
            getChildren(el[a].children);
            ....
            // then invoke the function expresion by adding ()
            // to the end. Pass in any arguments here also
        })(el.children,arg2,arg3);
    });
    return matched;
};

Tested in FF 76, MSIE 10, Chrome Canary 81

在 FF 76、MSIE 10、Chrome Canary 81 中测试