在“严格模式”下使用块内功能时,为什么Chrome仍保持静音? [重复]

时间:2022-09-04 11:08:06

This question already has an answer here:

这个问题在这里已有答案:

I am pretty new to JS "strict mode";, when I use code like:

我是JS“严格模式”的新手;当我使用代码时:

function outer(){

"use strict";
    var ctype;

    function inner(){

        if(ctype!=undefined){
            function hello1(){
                console.log("hello1");
            }
            hello1()
        }else {
            function hello2(){
                console.log("hello2");
            }
            hello2();
        }

    }

    return inner;

}

var inner = outer();

inner();

I wonder why Chrome(ver 49) give no error, but Node.js can give "SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function."

我想知道为什么Chrome(第49版)没有给出任何错误,但是Node.js可以给出“SyntaxError:在严格模式代码中,函数只能在*或者在另一个函数中声明。”

This table points out that my Chrome should report error.

此表指出我的Chrome应报告错误。

1 个解决方案

#1


10  

The version of Node.js you're using (0.12.9) uses an older version of the V8 JavaScript engine (3.28.71.19) which does not follow the new function declaration scoping rules added in ECMAScript 6. The version of Chrome you're using (49) uses a new version of V8 (4.9.385) which does support the new rules, at least in strict mode.

您正在使用的Node.js版本(0.12.9)使用较旧版本的V8 JavaScript引擎(3.28.71.19),该版本不遵循ECMAScript 6中添加的新功能声明范围规则。您的Chrome版本重新使用(49)使用新版本的V8(4.9.385),它支持新规则,至少在严格模式下。

Prior to ECMAScript 6, function declarations would be scoped to the containing function. For example:

在ECMAScript 6之前,函数声明将作用于包含函数。例如:

ECMAScript 5

function main() {
  if (true) {
    function example() {};

    console.log(example); // function example() {}
  }

  console.log(example); // function example() {}
}

This was considered confusing behaviour, and so it was prohibited in strict mode, leading to the error you see in Node.

这被认为是令人困惑的行为,因此在严格模式下被禁止,导致您在Node中看到的错误。

In ECMAScript 6, function declarations are instead scoped to the nearest block. A block is any series of statements contain between two brackets ({ ... }), such as following an if statement.

在ECMAScript 6中,函数声明的范围改为最近的块。块是包含两个括号({...})之间的任何一系列语句,例如跟在if语句之后。

ECMAScript 6

function main() {
  'use strict';

  if (true) {
    function example() {};

    console.log(example); // function example() {}
  }

  console.log(example); // ReferenceError: example is not defined
}

This behaviour is more intentional and less confusing, so it is permitted in strict mode.

这种行为更有意,更少混淆,因此在严格模式下是允许的。

However, testing this is a little bit confusing, because Chrome currently enables some ES6 rules only in strict mode.

但是,对此进行测试有点令人困惑,因为Chrome目前仅在严格模式下启用某些ES6规则。

#1


10  

The version of Node.js you're using (0.12.9) uses an older version of the V8 JavaScript engine (3.28.71.19) which does not follow the new function declaration scoping rules added in ECMAScript 6. The version of Chrome you're using (49) uses a new version of V8 (4.9.385) which does support the new rules, at least in strict mode.

您正在使用的Node.js版本(0.12.9)使用较旧版本的V8 JavaScript引擎(3.28.71.19),该版本不遵循ECMAScript 6中添加的新功能声明范围规则。您的Chrome版本重新使用(49)使用新版本的V8(4.9.385),它支持新规则,至少在严格模式下。

Prior to ECMAScript 6, function declarations would be scoped to the containing function. For example:

在ECMAScript 6之前,函数声明将作用于包含函数。例如:

ECMAScript 5

function main() {
  if (true) {
    function example() {};

    console.log(example); // function example() {}
  }

  console.log(example); // function example() {}
}

This was considered confusing behaviour, and so it was prohibited in strict mode, leading to the error you see in Node.

这被认为是令人困惑的行为,因此在严格模式下被禁止,导致您在Node中看到的错误。

In ECMAScript 6, function declarations are instead scoped to the nearest block. A block is any series of statements contain between two brackets ({ ... }), such as following an if statement.

在ECMAScript 6中,函数声明的范围改为最近的块。块是包含两个括号({...})之间的任何一系列语句,例如跟在if语句之后。

ECMAScript 6

function main() {
  'use strict';

  if (true) {
    function example() {};

    console.log(example); // function example() {}
  }

  console.log(example); // ReferenceError: example is not defined
}

This behaviour is more intentional and less confusing, so it is permitted in strict mode.

这种行为更有意,更少混淆,因此在严格模式下是允许的。

However, testing this is a little bit confusing, because Chrome currently enables some ES6 rules only in strict mode.

但是,对此进行测试有点令人困惑,因为Chrome目前仅在严格模式下启用某些ES6规则。