自调用匿名函数表达式 [英] A self invoking anonymous function expression

查看:61
本文介绍了自调用匿名函数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(function(){ ... })();

我查看了这篇帖子,并对它有所了解.但是几乎没有疑问,主要是如何使用它.

I HAVE looked at this post and understood a bit about it. But there are few more doubts, mostly on how it is used.

由于它的作用类似于静态块(自我调用!),因此可以用于初始化(例如某些make-believe常量)?

since it acts like a static block (self invoking!), it can be used for initializing(like some make-believe constants)?

但是没有getter可以从中获取任何东西并在其他地方使用!

But then there is no getter available to fetch anything from it and use it elsewhere!

上述解决方案是在该功能中使用return吗?这样我就可以获取返回的任何内容并使用它.

The solution to above is to HAVE a return in that function? so that I can fetch whatever it returns and use that.

(function(window, undefined){})(this);

上面代码的解释是在引用的 post 的第二个答案中,它,如果有人可以解释得更多(或对我来说更简单),那将很棒

The explanation for the above code was in the second answer of the referenced post, I couldn't understand it, If anyone can explain it more (or simpler for me), It will be great

var myElement=document.getElemetById("myElementId");
 (function(myElement){
      /**'this' here is 'myelement'???**/
 }; 
})(this);

推荐答案

常见的方法如下(称为命名空间)-它通过立即执行函数并将所需的必要部分返回到变量中来创建封装的作用域:

A common metod is the following (called namespacing) - It created an encapsulated scope by immediately executing your function and returning the essential parts you need into your variable:

var yourNamespace = (function(window, undefined){ 
                    /* private code:*/
                    var privateVar = "foobar",
                        count = 0;
                    /* the stuff you want to use outside: */
                    return{
                       val: 5,
                       publicVar:privateVar,
                       func:function(){return ++count}
                    }
             })(this);// `this` is a reference to `window` here

通过这种方式,您可以通过yourNamespace变量访问所需的所有内容,同时仍然保持隐私并且不会污染全局对象.这称为命名空间,并使用闭包范式.您还可以移交函数以使用私有(对于封闭的范围不可见)变量.

This way you have access to everything you need via your yourNamespace variable, while still maintaining privacy and not polluting the global object. It's called namespacing and uses the closure paradigm. You can also hand over functions to work with private (for the enclosing scope non-visible) variables.

移交undefined的一个原因是,在ES3中,undefined是可写的,而在ES5中则不再是这种情况.将this作为参数移交可以通过在范围内创建对全局window对象的直接引用来缩短查找.但是,请务必谨慎-在ES5严格模式下,this不再是窗口,而是解析为undefined!因此,不再建议这样做!

One reason for handing over undefined was, that in ES3 undefined was writable, which it is not the case anymore in ES5. Handing over over this as parameter shortens the lookup by creating a direct reference to the global window object in the scope. However, be very cautious - in ES5 strict mode this is not the window anymore but resolves to undefined!! So this is not recommended anymore!

移交窗口和未定义窗口的另一个原因是,既然它们是变量名,那么压缩程序可以将它们压缩为单个字母.

Another reason for handing over window and undefined is that now that these are variable names, a minifier could compress them to a single letter.

if(myVar == undefined)
// could be compressed to:
if(a==x)

编辑您的问题:

在您的示例中,this不会更改,您需要以下解决方案之一:

The this will not change in your example, you need one of the following solutions:

(function(myElement){/*your code*/})( document.getElemetById("myElementId") );
// or:
(function(){
    var myElement = document.getElemetById("myElementId");
    /* your code */ 
})();

这篇关于自调用匿名函数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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