我应该为我写的每一个javascript函数'使用严格'吗? [英] Should I 'use strict' for every single javascript function I write?

查看:95
本文介绍了我应该为我写的每一个javascript函数'使用严格'吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该为我编写的每个javascript函数使用strict吗?



在大型AngularJS项目中使用strict有什么好的做法?在全球范围内使用它可能会破坏不支持它的第三方库,但每次执行严格使用只是重复次数。

解决方案

关于这个问题,请注意过度简化的一般倾向。



首先,你的所有代码绝对应该运行严格模式。核心现代JavaScript功能已更改(请参阅 .call( )和apply())或毁容(无声错误)通过执行严格模式之外的代码。 (更多关于此此处,来自Crockford。)



但是,这并不能解决如何你应该确保你的代码在严格模式下运行。至少有两种情况需要考虑:



在浏览器中,您的代码应在缩小后传递。如果在每个函数体中都包含'use strict',那么你的minifier就不会将它删除掉,并且你会浪费在任何地方重复它的字节。您只需要在最外面的函数范围中 - 在模块定义的顶部。一种方法是将缩小的代码包装在单个IIFE闭包中作为构建过程的一部分:

 ;(function(){ 
'使用严格'
//此处所有代码。
}())

我认为这接近理想的做法,但它或多或少要求你采用持续的集成工作流程(因为要观察你的代码在严格模式下运行,你必须拥有它包裹一个封闭)。如果你不这样做,你必须在每个未在另一个函数范围内声明的函数的顶部包含 use strict 指令。 / p>

在服务器上,当然要简单得多。代码没有缩小,字节无关紧要,因此您只需在每个文件的顶部包含 use strict 指令。



上的警告词 - use_strict :如果您控制脚本的运行方式,您可能会发现自己试图使用 - use_strict 运行时标志。这很容易,但这意味着所有依赖必须符合严格模式。由于您无法控制每个第三方的合规性,这通常是不明智的。


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 a third party library that doesn't support it, but doing 'use strict' every single time is just a lot of repetition.

解决方案

On this question, do beware a general tendency to oversimplify.

First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see .call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode. (More on this here, from Crockford.)

However, that doesn't address how you should ensure that your code runs in strict mode. There are at least two contexts to consider:

In the browser, your code should be delivered after being minified. If you include 'use strict' in every function body, your minifier won't strip it out, and you'll waste bytes repeating it everywhere. You only need it in your outermost function scope(s)—at the top of your module definitions. One approach is to wrap your minified code in a single IIFE closure as part of the build process:

;(function (){ 
  'use strict'
  // All code here.
}())

This, I think, is close to the ideal way of doing it, but it more or less dictates that you adopt a continuous integration workflow (since, to observe your code running in strict mode, you must have it all wrapped up one closure). If you don't work that way, you'll have to include the use strict directive at the top of every function that isn't being declared within another function's scope.

On the server, it's much simpler, of course. Code isn't minified, and bytes don't matter, so you can just include the use strict directive at the top of every file.

A word of warning on --use_strict: In cases where you control how scripts are run, you may find yourself tempted to use the --use_strict runtime flag. It's easy, but it means all dependencies must be strict mode compliant. Since you can't control every third-party's compliance, this is usually unwise.

这篇关于我应该为我写的每一个javascript函数'使用严格'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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