简体   繁体   English

这种函数在javascript中意味着什么?

[英]What does this type of function mean in javascript?

In the below, I'm confused by the syntax 在下面,我对语法感到困惑

(function (h,j) { })

What does it mean in javascript to have a function sit inside ()'s like that? 在javascript中有一个函数坐在里面()是什么意思?

function myfunc(c, b) {
    try {
            (function (h, j) {
                //do a bunch of stuff
            })
    } catch (e) {
        myerror(e)
    }
};

By itself, such a function declaration is useless. 就其本身而言,这样的函数声明是无用的。 This kind of declaration is only useful if you actually invoke the function, which is done like this: 这种声明只有在您实际调用函数时才有用,如下所示:

(function (h, j) { ... } (x, y));

This is usually done to hide variables, since JavaScript only has function scope (no block scope). 这通常用于隐藏变量,因为JavaScript只有函数作用域(没有块作用域)。

Edit: 编辑:

Some examples - hopefully these don't confuse things... 一些例子 - 希望这些不会混淆事物......

As mentioned in a comment, this technique is useful for keeping variables out of the global scope. 正如评论中所提到的,此技术对于将变量保持在全局范围之外非常有用。 For example, some initialisation script might do the following: 例如,某些初始化脚本可能会执行以下操作:

(function () {
    var x = 42;
    var y = 'foo';

    function doInitialisation(a, b) {
        // ...
    }

    doInitialisation(x, y);
}());

None of x , y or doInitialisation are visible after the function completes. 在函数完成后, xydoInitialisation都不可见。

Another use-case is for avoiding closures in loops. 另一个用例是避免循环中的闭包。 Eg the following causes a well-known problem: 例如,以下是一个众所周知的问题:

var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
    links[i].onclick = function () {
        alert(i);
    };
}

In the above example, every onclick handler shares the same value of i . 在上面的示例中,每个onclick处理程序共享相同的i值。 Function scope can avoid this: 功能范围可以避免这个:

var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
    links[i].onclick = (function (x) {
        return function() {
            alert(x);
        }
    }(i));
}

() is a grouping operator, and it returns the result of evaluating the expression inside it. ()是一个分组运算符,它返回计算其中表达式的结果。

So while 所以虽然

> function(x,y) {}
SyntaxError: Unexpected token (

by itself is a SyntaxError , but by surrounding it in parentheses, the expression inside the parentheses is evaluated and returned. 它本身就是一个SyntaxError ,但是通过将它括在括号中,括号内的表达式将被计算并返回。

> (function(x,y) {})
function (x,y) {}

Function expressions and declarations do not yield any value, so we get undefined as a result. 函数表达式和声明不会产生任何值,因此我们得到了undefined的结果。

Function Declaration 功能声明

> function a(x,y) {}
undefined

Function Declaration (with grouping operator) 功能声明(带分组操作员)

(function a(x,y) {})
function a(x,y) {}

Function Expression 功能表达

> var x = function(x,y) {}
undefined

Function Expression (with grouping operator) 函数表达式(带分组运算符)

> var x;
> (x = function(x,y) {})
function (x,y) {}

However, the usage in your example seems to be useless. 但是,您示例中的用法似乎毫无用处。 It does nothing. 它什么都不做。

That syntax is what is known as an Anonymous Function . 该语法就是所谓的匿名函数 Most often, you will see them used as callbacks for various other function calls (for example, in jQuery). 大多数情况下,您会看到它们用作各种其他函数调用的回调(例如,在jQuery中)。

It's a kind of inline function, so you can take the advantages of covariance. 它是一种内联函数,因此您可以利用协方差的优势。 By this I mean,inside 我的意思是,在里面

(function (h, j) { //do a bunch of stuff }) You can access the variables of the containing function , here it's function myfunc(c, b) {} (function(h,j){//做一堆东西})你可以访问包含函数的变量,这里是function myfunc(c, b) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM