代码之家  ›  专栏  ›  技术社区  ›  El Ronnoco

如何/何时调用此匿名函数?

  •  1
  • El Ronnoco  · 技术社区  · 14 年前

    我对javascript是个新手,我正在浏览一些Raphael演示代码。我对这是如何工作感到困惑…

    if (R) {
        (function (dx, dy, R, value) {
            var color = "hsb(" + [(1 - R / max) * .5, 1, .75] + ")";
    ...
    

    据我所见,这是声明一个匿名函数,它接受4个参数。当此函数没有名称时,如何调用此函数??

    演示页面。 http://raphaeljs.com/github/dots.html

    JS文件。 http://raphaeljs.com/github/dots.js

    4 回复  |  直到 14 年前
        1
  •  2
  •   sorpigal    14 年前

    })(leftgutter + X * (j + .5) - 60 - R, Y * (i + .5) - 10, R, data[o]);
    

    } ) (function

    (function(arg){ alert(arg); })("Hi!");
    
        2
  •  1
  •   lincolnk    14 年前

    (function (dx, dy, R, value) {
    
    // ... stuff
    
    dot[0].onmouseover = function () {
        if (bg) {
            bg.show();
        } else {
            var clr = Raphael.rgb2hsb(color);
            clr.b = .5;
            dt.attr("fill", Raphael.hsb2rgb(clr).hex);
        }
        lbl.show();
    };
    
    // more stuff
    
    })(leftgutter + X * (j + .5) - 60 - R, Y * (i + .5) - 10, R, data[o]);
    

        3
  •  1
  •   Ara    14 年前

    function(a){<some method code>}(x);
    

    x a

    (function (dx, dy, R, value) {
        var color = "hsb(" + [(1 - R / max) * .5, 1, .75] + ")";
        ...
    })(leftgutter + X * (j + .5) - 60 - R, Y * (i + .5) - 10, R, data[o]);
    
        4
  •  1
  •   Josh Lee ZZ Coder    14 年前

    ...
        })(leftgutter + X * (j + .5) - 60 - R, Y * (i + .5) - 10, R, data[o]);
    

    window.addEventListener("load", function(event) {
        document.body.innerHTML = "hello";
    }, false);
    

    { }

    (function() {
        var foo; // guaranteed to not leak or interfere with someone else's foo
        // 6000 lines ...
    })();
    

    JavaScript Scope and Closures