Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: use strict is unnecessary inside of modules

Tags:

javascript

tsling raise an error:

Line 1: 'use strict' is unnecessary inside of modules (strict)

this is my code

"use strict";

function Foo() {}

Foo.prototype.sayHello= function () {
    console.log("hello!");
}

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    module.exports = { 
        Foo: Foo
    };
}

how fix this error?

Side note

my code is used both module and vanilla javascript. I want use "strict mode" only for vanilla javascript.

maybe i can use

if (typeof module !== 'undefined') {
    "use strict";
}

for enabling strict mode only for vanilla javascript?

like image 220
ar099968 Avatar asked Mar 20 '18 13:03

ar099968


People also ask

Is use strict still necessary?

Is use strict necessary? The strict mode is no longer required since the release of ES2015, which fixes most of JavaScript's confusing behavior with a more robust syntax. It's just good to know because you might find it in legacy projects that still used it.

Is use strict necessary in ES6?

Strict Mode. Strict Mode(“use strict”) helps identify common issues (or “bad” parts) and also helps with “securing” JavaScript. In ES5, the Strict Mode is optional but in ES6, it's needed for many ES6 features.

Where should we place use strict in JavaScript?

The JavaScript strict mode is a feature in ECMAScript 5. You can enable the strict mode by declaring this in the top of your script/function. 'use strict'; When a JavaScript engine sees this directive, it will start to interpret the code in a special mode.

Should I use strict JavaScript?

Benefits of Strict Mode The use of strict mode: helps to write a cleaner code. changes previously accepted silent errors (bad syntax) into real errors and throws an error message. makes it easier to write "secure" JavaScript.


2 Answers

Remove 'use strict'. As the error mentions, it's unnecessary. Modules are expected to execute in strict mode. Compilers will add it for you when you export the module into a script for non-module consumption (i.e. UMD/CJS). See --alwaysStrict option for TS.

like image 111
Joseph Avatar answered Oct 30 '22 20:10

Joseph


ES6 modules are always in strict mode.

like image 22
Ginie Avatar answered Oct 30 '22 19:10

Ginie