【问题标题】:javascript: use strict is unnecessary inside of modulesjavascript:在模块内部不需要使用严格
【发布时间】:2018-08-29 08:58:04
【问题描述】:

tsling 报错:

第 1 行:模块内部不需要“使用严格”(严格)

这是我的代码

"use strict";

function Foo() {}

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

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

如何解决这个错误?

旁注

我的代码同时用于 module 和 vanilla javascript。我只想将 "strict mode" 用于香草 javascript。

也许我可以使用

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

只为 vanilla javascript 启用 strict mode

【问题讨论】:

标签: javascript


【解决方案1】:

删除'use strict'。正如错误所述,这是不必要的。模块应该在严格模式下执行。当您将模块导出到脚本以供非模块使用(即 UMD/CJS)时,编译器会为您添加它。请参阅--alwaysStrict TS 选项。

【讨论】:

  • 好的,但是我的代码在模块和香草 javascript 中都使用了
【解决方案2】:

ES6 模块始终处于严格模式。

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 2014-05-22
    • 2018-02-08
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2016-06-19
    • 2015-05-06
    相关资源
    最近更新 更多