Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 命名函数和匿名函数具有不同的效果_Javascript_Backbone.js_Refactoring_Closures_Anonymous Function - Fatal编程技术网

Javascript 命名函数和匿名函数具有不同的效果

Javascript 命名函数和匿名函数具有不同的效果,javascript,backbone.js,refactoring,closures,anonymous-function,Javascript,Backbone.js,Refactoring,Closures,Anonymous Function,我的问题: 我正在重构我的一些代码,并为一些长的匿名函数命名。不幸的是,它以我不理解的方式破坏了应用程序 代码 匿名版本很好用 警报(分配器长度) 与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 di

我的问题:

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

代码

匿名版本很好用

警报(分配器长度)

与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" );
                    }
                ) );
                }
            );
            }
        );
命名版本:它没有任何功能

警报(分配器长度)

值始终为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()
        );
我的问题


为什么这两个函数的行为不同,我应该如何声明我的命名函数以使其行为类似于匿名函数。

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

实际上,在这两种情况下,您的函数都是匿名的。在第二种情况下,只需将函数的引用指定给变量。要使函数不匿名,可以使用函数声明:

。。。或使用:


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

实际上,在这两种情况下,您的函数都是匿名的。在第二种情况下,只需将函数的引用指定给变量。要使函数不匿名,可以使用函数声明:

。。。或使用:


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

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