用“((function(){.... [这里的js代码] .........})();"包装有角的javascript文件有什么好处? [英] Are there any benefits to wrapping angular javascript file with the "(function() { ....[js code here]...... })();"

查看:49
本文介绍了用“((function(){.... [这里的js代码] .........})();"包装有角的javascript文件有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道的简单问题.

Simple questions I've always wondered.

我从没有香草javascript背景的角度开始,我很确定是这样,但是有时候我看到具有以下内容的javascript文件:

I started with angular without a vanilla javascript background I'm pretty sure that's that this is, but sometimes I see javascript files that have the:

(function() {
   'use strict';
   ....
})();

像这样的文件的开头和结尾:

at the start and finish of a file like:

(function() {
   'use strict';

   angular.module('asdf', []);
   angular.controller('asdf' function(){.....})
})();

我不使用它,并且一切运行正常.所以我的问题是,将我的javascript文件包装在其中是否有任何好处?

I don't use it and and everything runs fine. So my question are there any benefits by wrapping my javascript files inside of them?

推荐答案

因此,您正在寻找的是一个立即调用的匿名函数.

So, what you're looking at there is an imediately invoked anonymous function.

基本上,使用此模式",因为如果不使用它,则是在全局名称空间中工作,即开即用的javascript可能会导致不必要的问题,包括第三方库/plugins或您编写的其他模块.此处此处

Basically, this "pattern" is used, because if you're not using it, you're working in the global namespace javascript out-of-the-box runs in, which could lead to unwanted problems including third party libraries/plugins or other modules you wrote. More insight here, there or here

这篇关于用“((function(){.... [这里的js代码] .........})();"包装有角的javascript文件有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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