如何使用(function(global){...})(this);

时间:2022-05-31 15:44:59

In this thread I found a JavaScript code snippet which I want to use.

在这个帖子中,我找到了一个我想要使用的JavaScript代码片段。

The code looks like:

代码如下:

(function(global) {
  // the function code comes here
})(this);

How can i call this function to execute the code? What do I have to pass in for this global variable?

如何调用此函数来执行代码?我必须为这个全局变量传递什么?

3 个解决方案

#1


18  

The function is immediately executed, you do not execute it by calling it.

该函数立即执行,您不通过调用它来执行它。

It is a function literal definition, followed by two parens which causes that function to invoke immediately. Read more: Immediately-Invoked Function Expression (IIFE)

它是一个函数文字定义,后跟两个parens,导致该函数立即调用。阅读更多:立即调用函数表达式(IIFE)

Whatever code you place inside is run right away. Anything placed in the invocation parens is passed into the function as an argument. Assuming your sample code was defined in the global scope, this is the window object, and is referenced as global within the function body. It is a great way to encapsulate your programs to avoid variable collision, force strict mode, and much more.

无论你在里面放置什么代码都可以立即运行。放置在调用parens中的任何内容都将作为参数传递给函数。假设您的示例代码是在全局范围内定义的,那么这是窗口对象,并在函数体中被引用为全局。这是封装程序以避免变量冲突,强制严格模式等的好方法。

#2


15  

This construct defines a function:

此构造定义了一个函数:

function(global) {
  // the function code comes here
}

and immediately calls it, passing this as a parameter:

并立即调用它,将其作为参数传递:

([function])(this)

The identifier global is simply the name of this parameter inside the function body. For example, try

标识符global只是函数体内该参数的名称。例如,试试

console.log(this); // outputs something
(function(global) {
    console.log(global); // outputs the same thing as above
})(this);

#3


1  

How can i call this function to execute the code?

如何调用此函数来执行代码?

It is already being called: (this)

它已被称为:(这)

What do I have to pass in for this global variable?

我必须为这个全局变量传递什么?

this

这个

#1


18  

The function is immediately executed, you do not execute it by calling it.

该函数立即执行,您不通过调用它来执行它。

It is a function literal definition, followed by two parens which causes that function to invoke immediately. Read more: Immediately-Invoked Function Expression (IIFE)

它是一个函数文字定义,后跟两个parens,导致该函数立即调用。阅读更多:立即调用函数表达式(IIFE)

Whatever code you place inside is run right away. Anything placed in the invocation parens is passed into the function as an argument. Assuming your sample code was defined in the global scope, this is the window object, and is referenced as global within the function body. It is a great way to encapsulate your programs to avoid variable collision, force strict mode, and much more.

无论你在里面放置什么代码都可以立即运行。放置在调用parens中的任何内容都将作为参数传递给函数。假设您的示例代码是在全局范围内定义的,那么这是窗口对象,并在函数体中被引用为全局。这是封装程序以避免变量冲突,强制严格模式等的好方法。

#2


15  

This construct defines a function:

此构造定义了一个函数:

function(global) {
  // the function code comes here
}

and immediately calls it, passing this as a parameter:

并立即调用它,将其作为参数传递:

([function])(this)

The identifier global is simply the name of this parameter inside the function body. For example, try

标识符global只是函数体内该参数的名称。例如,试试

console.log(this); // outputs something
(function(global) {
    console.log(global); // outputs the same thing as above
})(this);

#3


1  

How can i call this function to execute the code?

如何调用此函数来执行代码?

It is already being called: (this)

它已被称为:(这)

What do I have to pass in for this global variable?

我必须为这个全局变量传递什么?

this

这个