【问题标题】:Named function and anonymous function having different effects命名函数和匿名函数有不同的效果
【发布时间】:2013-05-27 02:25:57
【问题描述】:

我的问题:

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

代码

匿名版本可以正常工作,

alert(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" );
                    }
                ) );
                }
            );
            }
        );

命名版本:它什么都不做

alert(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()
        );

我的问题

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

【问题讨论】:

    标签: javascript backbone.js refactoring closures anonymous-function


    【解决方案1】:

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

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

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

    ...或使用named function expression:

    var addCollections = function someName() {
        // someName is now a reference to the function, but only
        // within the function
    };
    

    【讨论】:

      【解决方案2】:

      这不是一个命名函数,您将一个函数分配给一个名为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
                  );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 2022-07-06
        • 2017-04-04
        • 2013-03-29
        • 1970-01-01
        • 1970-01-01
        • 2020-01-03
        相关资源
        最近更新 更多