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

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

问题描述

当您将 JavaScript 代码包装在这样的函数中时:

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天全站免登陆