Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Chrome still keep silent when using functions inside blocks in "strict mode"? [duplicate]

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

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."

This table points out that my Chrome should report error.

like image 804
Kuan Avatar asked Apr 20 '16 22:04

Kuan


People also ask

When would you not use strict mode?

If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.

What are the advantages and disadvantages of using use strict?

what are the advantages and disadvantages to using it? If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.

What are the effects of having use strict?

The "use strict" Directive It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables.

What is correct way to run a JavaScript in strict mode?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.

How to stop chrome background process when Chrome is closed?

Stop Chrome Background Process 1 Click the three-dot icon on Google Chrome. 2 Then click Settings. 3 In the pop-up window, click Advanced to continue. 4 Then toggle off the option Continue running background apps when Google Chrome is closed. More ...

Do all browsers have strict mode now?

The major browsers now implement strict mode. However, don't blindly depend on it since there still are numerous Browser versions used in the wild that only have partial support for strict mode or do not support it at all (e. g. Internet Explorer below version 10!). Strict mode changes semantics.

What is strict mode in JavaScript?

First, strict mode makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work" (although future failure is possible: likely, in modern JavaScript).

Why does Google Chrome keep running after I close it?

In order to provide more functionality, some apps can keep running even after you close the last window of Google Chrome. These are called background apps. To indicate that a process is still running, a Google Chrome icon will appear in the system tray on Windows and the dock on Mac.


1 Answers

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.

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

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.

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

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.

like image 56
Jeremy Avatar answered Nov 02 '22 23:11

Jeremy