使用“function()”使用严格''的好处是什么?在每个文件? [英] What's the benefit of using "function() 'use strict'" in every file?

查看:63
本文介绍了使用“function()”使用严格''的好处是什么?在每个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构旧的AngularJS 1.3项目。我注意到的一件事是,制作它的人开始使用AngularJS代码的每个文件:

I'm refactoring an old AngularJS 1.3 project. One of the things I noticed is that the person who made this started every single file of AngularJS code with:

(function () {
'use strict';

  angular.module('app').factory('Employees', ['$http', function($http) {
    // angular code removed
  }]);

})();

在每个文件中使用function()'use strict'对代码有什么好处?对我而言,每个文件中都浪费了3行。是否有标准/最佳实践?

Does using function() 'use strict' in every single file any benefit to the code? To me it feels like a waste of 3 lines in every single file. Is there a standard / best practice for this?

推荐答案

如果你把'使用严格'; 在IIFE之外你将它变为全局。

If you put 'use strict'; outside the IIFE you make it global.

这意味着你可能无法控制的代码(例如其他库)严格遵守可能会导致问题。

That means it is possible that code you have no control over (other libraries for example) that isn't strict compliant might cause problems.

就像变量范围一样,你只能在范围内'使用严格'代码的上下文只能将它全部放在IIFE

Just like variable scope, you are only scoping 'use strict' within the context of your code only by putting it all inside the IIFE

这篇关于使用“function()”使用严格''的好处是什么?在每个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆