令人费解的函数构造函数 [英] puzzling function constructor

查看:87
本文介绍了令人费解的函数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在js文件中遇到过这段代码。

它以我从未见过的方式使用函数构造函数。

有人可以解释这里发生了什么?

谢谢


(功能(){

...代码行......

})();

I have come across this code in a js file.
It uses the function constructor in a way I have never seen.
Can someone explain what is happening here?
Thanks

(function(){
...lines of code...
} ) ();

推荐答案

6月24日晚上8:34,noddy< n ... @ toyland.comwrote:
On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:

我在js文件中遇到过这段代码。

它以我从未见过的方式使用函数构造函数。

有人可以解释这里发生了什么吗?

谢谢


(功能(){

...代码行...


})(); - 隐藏引用的文字 -


- 显示引用的文字 -
I have come across this code in a js file.
It uses the function constructor in a way I have never seen.
Can someone explain what is happening here?
Thanks

(function(){
...lines of code...

} ) ();- Hide quoted text -

- Show quoted text -



它定义了一个匿名函数,然后立即调用它。如你所知,如果''foo''是一个函数,那么你可以用代码

''foo()''来调用它。然而,在JavaScript中,函数是一流的(就像

字符串或整数)并且不必有名字。所以函数()

{...代码行...}实际上是一个函数,就像2字面上是一个

数字或''bar''字面意思一个字符串。所以你可以调用函数()

{...代码行...}就像你命名的那样:通过附加

''()' ' 在末尾。他们可能已经将代码编写为函数

(){...代码行...}()并获得相同的效果。另外,如果

函数接受诸如函数(x,y,z)之类的参数{...行的

代码...}那么你可以调用它代码如(function(x,y,z)

{...})(a,b,c)其中a,b和c是合法的JavaScript值。

It is defining an anonymous function then immediately calling it. As
you know, if ''foo'' is a function then you can call it with the code
''foo()''. In JavaScript, however, functions are first class (just like
strings or integers) and don''t have to have names. So function ()
{...lines of code...} is literally a function just as 2 is literally a
number or ''bar'' is literally a string. So you can call function ()
{...lines of code...} just as you would if it were named: by appending
''()'' at the end. They could have probably written the code as function
() {...lines of code...}() and gotten the same effect. Also, if the
function takes arguments such as function (x, y, z) {...lines of
code...} then you can call it with code such as (function (x,y,z)
{...})(a,b,c) where a, b, and c are legal JavaScript values.


6月25日下午12:47,Rod< noblethras ... @ gmail.comwrote:
On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:

6月24日晚上8:34,noddy< n ... @ toyland.comwrote:
On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:

我在js文件中遇到过这段代码。

它以我从未见过的方式使用函数构造函数。

有人可以解释这里发生了什么吗?

谢谢
I have come across this code in a js file.
It uses the function constructor in a way I have never seen.
Can someone explain what is happening here?
Thanks


(function(){

...代码行...
(function(){
...lines of code...


})(); - 隐藏引用的文字 -
} ) ();- Hide quoted text -


- 显示引用的文字 -
- Show quoted text -



它正在定义一个匿名函数,然后立即调用它。如你所知,如果''foo''是一个函数,那么你可以用代码

''foo()''来调用它。然而,在JavaScript中,函数是一流的(就像

字符串或整数)并且不必有名字。所以函数()

{...代码行...}实际上是一个函数,就像2字面上是一个

数字或''bar''字面意思一个字符串。所以你可以调用函数()

{...代码行...}就像你命名的那样:通过附加

''()' ' 在末尾。他们可能已经将代码编写为函数

(){...代码行...}()并获得相同的效果。


It is defining an anonymous function then immediately calling it. As
you know, if ''foo'' is a function then you can call it with the code
''foo()''. In JavaScript, however, functions are first class (just like
strings or integers) and don''t have to have names. So function ()
{...lines of code...} is literally a function just as 2 is literally a
number or ''bar'' is literally a string. So you can call function ()
{...lines of code...} just as you would if it were named: by appending
''()'' at the end. They could have probably written the code as function
() {...lines of code...}() and gotten the same effect.



不,他们不能。如果声明以标识符

" function"开头。它将被解释为函数*声明*

必须具有名称,否则将导致语法错误。您可以在函数*表达式*中省略

的名称,因此使用:


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

如果理查德·科尔福德还在潜伏着你,可能会让他退出

退休。


-

Rob

No, they couldn''t. If a statement starts with the identifier
"function" it will be interpreted as a function *declaration* which
must have a name, otherwise a syntax error will result. You can omit
the name in a function *expression*, hence the use of:

(function(){...})();
If Richard Cornford is still lurking you may have coaxed him out of
retirement.

--
Rob


On Sun,2007年6月24日20:17:08 -0700,RobG< rg *** @ iinet.net.auwrote :
On Sun, 24 Jun 2007 20:17:08 -0700, RobG <rg***@iinet.net.auwrote:

> 6月25日下午12:47,Rod< noblethras ... @ gmail.comwrote:
>On Jun 25, 12:47 pm, Rod <noblethras...@gmail.comwrote:

> 6月24日晚上8:34,noddy< n ... @ toyland.comwrote:
>On Jun 24, 8:34 pm, noddy <n...@toyland.comwrote:

我遇到过这个js文件中的代码。

它以我从未见过的方式使用函数构造函数。

有人可以解释这里发生了什么吗?

谢谢
I have come across this code in a js file.
It uses the function constructor in a way I have never seen.
Can someone explain what is happening here?
Thanks


(function(){

...代码行...
(function(){
...lines of code...


})(); - 隐藏引用的文字 -
} ) ();- Hide quoted text -


- 显示引用的文字 -
- Show quoted text -


它正在定义一个匿名函数然后立即叫它。如你所知,如果''foo''是一个函数,那么你可以用代码
''foo()''来调用它。然而,在JavaScript中,函数是第一类的(就像
字符串或整数一样)并且不必具有名称。所以函数()
{...代码行...}实际上是一个函数,就像2字面上是数字或bar字面上的字符串一样。所以你可以调用function()
{...代码行...}就像你命名的那样:在最后附加
''()''。他们可能已经将代码编写为函数
(){...代码行...}()并获得相同的效果。


It is defining an anonymous function then immediately calling it. As
you know, if ''foo'' is a function then you can call it with the code
''foo()''. In JavaScript, however, functions are first class (just like
strings or integers) and don''t have to have names. So function ()
{...lines of code...} is literally a function just as 2 is literally a
number or ''bar'' is literally a string. So you can call function ()
{...lines of code...} just as you would if it were named: by appending
''()'' at the end. They could have probably written the code as function
() {...lines of code...}() and gotten the same effect.


不,他们不能。如果语句以标识符
function开头,则以语句开头。它将被解释为一个函数*声明*
必须有一个名称,否则将导致语法错误。您可以省略函数*表达式*中的名称,因此使用:


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

如果理查德·科尔福德还在潜伏着你,可能会让他退休。


No, they couldn''t. If a statement starts with the identifier
"function" it will be interpreted as a function *declaration* which
must have a name, otherwise a syntax error will result. You can omit
the name in a function *expression*, hence the use of:

(function(){...})();
If Richard Cornford is still lurking you may have coaxed him out of
retirement.



我可以理解匿名函数调用

function(){...}


令我困惑的是周围的括号和下面的();

你能解释一下这些吗?


I can understand the anonymous function call
function(){...}

what puzzles me is the surrounding brackets and the following ();
can you explain what these achieve please ?


这篇关于令人费解的函数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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