Expression closures - use-cases for shortcut lambda syntax (blocks)

Robert Sayre sayrer at gmail.com
Fri Mar 16 18:57:41 PDT 2007


On 3/16/07, Brendan Eich <brendan at mozilla.org> wrote:
>  Pick use-cases that come from real code, Ruby or
> whatever. Don't use miniature fakes. Then perhaps we can come to
> agreement quickly.

Here are some examples from culled from the web for MochiKit's
addLoadEvent function:

addLoadEvent(function () {
    var elems = getElementsByTagAndClassName("A", "view-source");
    var page = "rounded_corners/";
    for (var i = 0; i < elems.length; i++) {
        var elem = elems[i];
        var href = elem.href.split(/\//).pop();
        elem.target = "_blank";
        elem.href = "../view-source/view-source.html#" + page + href;
    }
});

addLoadEvent(function() {
        $("debug").innerHTML = repr(getElementPosition($("street")));
        $("debug").innerHTML += "<br>";
        $("debug").innerHTML += repr(quirksmode.findPos($("street")));
});

addLoadEvent(function(){
  for(var _70 in DomDeco.registry){
    for(var i=0;i<DomDeco.registry[_70].length;i++){
      DomDeco.apply(_70,DomDeco.registry[_70][i]);
   }
  }
});

addLoadEvent(function () {
     var d = wait(0.5, {data: [["getUsers", "getUsers"], ["foo",  "bar"]]});
     d.addCallback(showSelectData);
     d.addErrback(showError);
});


Nested anonymous functions:
------------------------------------------
addLoadEvent(function(){
    connect('pagelist','onclick', function (e) {
    e.preventDefault();
    var d = loadJSONDoc("${std.url('/pagelist', tg_format='json')}");
    d.addCallback(showPageList);
    });
});

addLoadEvent(function(){
    connect('bad_example','onclick',
        function(e){signal('bad_example','showvalue',"explicit_value")});
    connect('bad_example','showvalue',window,'alert');

    connect('good_example','onclick',
        function(e){signal(randomObj,'showvalue',"explicit_value")});
    connect(randomObj,'showvalue',window,'alert');
});

Seems to me that "function(){" is a little more painful when nested.

-- 

Robert Sayre



More information about the Es4-discuss mailing list