0

我的问题:

我正在重构我的一些代码,并为一些长匿名函数命名。不幸的是,它以我不理解的方式破坏了应用程序。

编码

匿名版本运行良好,

警报(distributeurs.length);

不同于0。

 var group = this.settings.group, //group used to store all the markers added to the map
            leads = this.model.get("leads"), // collection of leads
            distributeurs = new Distributeurs(), // collection of distributeurs
            map = this.settings.map,
            addLeadsCollection = this.addLeadsCollectionFnContructor();


        //ajax calls to populate collection
        $.when(leads.fetch({ data: $.param({ departementCode: departementCode }) }), distributeurs.fetch({ data: $.param({ departementCode: departementCode }) })).done(
            function () //the function
            {
                alert( distributeurs.length ); //the alert
                distributeurs.map( function ( distributeur )
                {
                    addLeadsCollection( leads.filter( function ( lead )
                    {
                        return distributeur.get( "id" ) === lead.get( "distribution" );
                    }
                ) );
                }
            );
            }
        );

命名版本:它什么都不做

警报(distributeurs.length);

始终值为 0。

var group = this.settings.group, //group used to store all the markers added to the map
            leads = this.model.get("leads"), // collection of leads
            distributeurs = new Distributeurs(), // collection of distributeurs
            map = this.settings.map,
            addLeadsCollection = this.addLeadsCollectionFnContructor();



        //the function
        var addCollections = function() {
            alert(distributeurs.length); //the alert
            distributeurs.map(function(distributeur) {
                addLeadsCollection(leads.filter(function(lead) {
                    return distributeur.get("id") === lead.get("distribution");
                }
                ));
            }
            );
        };

        //ajax calls to populate collection
        $.when(leads.fetch({ data: $.param({ departementCode: departementCode }) }), distributeurs.fetch({ data: $.param({ departementCode: departementCode }) })).done(
            addCollections()
        );

我的问题

为什么这两个函数的行为不同,我应该如何声明我的命名函数以使其表现得像匿名函数一样。

4

2 回答 2

3

从 中删除括号addCollections()。您正在立即调用该函数;你想要做的是传递函数。

实际上,您的函数在这两种情况下都是匿名的。在第二种情况下,您所做的就是将对函数的引用分配给变量。要使函数不是匿名的,您可以使用函数声明

function addCollections() {
    // Stuff...
}

...或使用命名函数表达式

var addCollections = function someName() {
    // someName is now a reference to the function, but only
    // within the function
};
于 2013-05-31T09:45:18.347 回答
1

这不是一个命名函数,您正在将一个函数分配给一个名为addCollections. 您的问题是您正在调用该函数而不是在此处传递引用:

$.when(leads.fetch({ data: $.param({ departementCode: departementCode }) }), distributeurs.fetch({ data: $.param({ departementCode: departementCode }) })).done(
            addCollections()
        );

删除括号:

$.when(leads.fetch({ data: $.param({ departementCode: departementCode }) }), distributeurs.fetch({ data: $.param({ departementCode: departementCode }) })).done(
                addCollections
            );
于 2013-05-31T09:46:11.007 回答