这个做法在JavaScript中被称为什么? [英] What is this practice called in JavaScript?

查看:142
本文介绍了这个做法在JavaScript中被称为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (function(){

var field = ...;
function doSomthing(){...
...


})();

我注意到这在很多网页上修复了我的范围问题。

解决方案

该模式称为自我调用自调用功能。它可以创建一个关闭,但这是模式的效果(也许是预期的效果),而不是模式本身。


When you wrap your JavaScript code in a function like this:

(function(){

  var field = ...;
  function doSomthing(){...
  ...


})();

I noticed that this fixes scoping problems for me on a lot of web pages. What is this practice called?

解决方案

The pattern is called self-invocation, a self-invoking function. It can create a closure, but that is an effect of the pattern (perhaps the intended effect), not the pattern itself.

这篇关于这个做法在JavaScript中被称为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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