Should I 'use strict' for every single javascript function I write?

后端 未结 4 1109
逝去的感伤
逝去的感伤 2020-12-17 08:37

Should I \'use strict\' for every single javascript function I write?

What is a good practice to use strict in a large AngularJS project? Using it globally can break

4条回答
  •  没有蜡笔的小新
    2020-12-17 09:04

    All code you write1 should be in strict mode. It helps you catch mistakes by not ignoring exceptions.

    However, no, this does not mean that you need to prepend "use strict"; to every function definition, you only should put it in the module scope - once per file - so that it is inherited by all your functions. When you're going to switch to ES6 modules it will be implied anyway.

    1: I'd even argue for enabling it globally, as strict mode should not break any properly written code.
    If it does break a third party script, the solution might not be to disable strict mode again…

提交回复
热议问题