简体   繁体   English

如何在创建函数时不可用的回调函数中使用变量

[英]How to use a variable in a callback function that is not available when the function is created

I am learning about functional programming in Javascript and I am running into an error on the below code. 我正在学习Javascript中的函数编程,并且在以下代码中遇到错误。 The error says that list is undefined. 该错误表明该list未定义。 This is obviously coming from my filterVideos function because it uses list, but list has not yet been defined. 这显然来自我的filterVideos函数,因为它使用列表,但是列表尚未定义。 I know I can just place the function into my return statement, but I am trying to keep the function separate from the return statement. 我知道我可以将函数放入return语句中,但是我试图将函数与return语句分开。 So my question is, is there a way for me to declare the function before the list variable has been defined? 所以我的问题是,在定义列表变量之前,有没有办法让我声明该函数?

var myFunction = function() {
    var lists = [
            {
                "id": 5434364,
                "name": "New Releases"
            },
            {
                "id": 65456475,
                name: "Thrillers"
            }
        ],
        videos = [
            {
                "listId": 5434364,
                "id": 65432445,
                "title": "The Chamber"
            },
            {
                "listId": 5434364,
                "id": 675465,
                "title": "Fracture"
            },
            {
                "listId": 65456475,
                "id": 70111470,
                "title": "Die Hard"
            },
            {
                "listId": 65456475,
                "id": 654356453,
                "title": "Bad Boys"
            }
        ];

    var filterVideos = function(video){ return video.listId == list.id;};
    var mapVideos = function(video){return {id: video.id, title: video.title};};

    return lists.map(function(list) {
        return {
          name: list.name,
          videos: videos.filter(filterVideos).map(mapVideos)
        };
    });
}

myFunction();

You could change filterVideos so it generates a filtering function that has the list in scope: 您可以更改filterVideos,以便它生成具有范围内列表的过滤功能:

var myFunction = function() {
    var lists = [{
            "id": 5434364,
            "name": "New Releases"
        }, {
            "id": 65456475,
            name: "Thrillers"
        }],
        videos = [{
            "listId": 5434364,
            "id": 65432445,
            "title": "The Chamber"
        }, {
            "listId": 5434364,
            "id": 675465,
            "title": "Fracture"
        }, {
            "listId": 65456475,
            "id": 70111470,
            "title": "Die Hard"
        }, {
            "listId": 65456475,
            "id": 654356453,
            "title": "Bad Boys"
        }];

    var filterVideos = function(list) {
        return function(video) {
            return video.listId == list.id;
        };
    };
    var mapVideos = function(video) {
        return {
            id: video.id,
            title: video.title
        };
    };

    return lists.map(function(list) {
        return {
            name: list.name,
            videos: videos.filter(filterVideos(list)).map(mapVideos)
        };
    });
}

myFunction();

As you say, the error is because list isn't defined at that point in the code. 就像您说的那样,该错误是因为列表尚未在代码中定义。 The two obvious solutions are either to either move it into a scope where it is defined (which you say you don't want to do), or pass it as a parameter to the filterVideos function so that it's available within. 两种明显的解决方案是将其移到定义它的范围内(您说您不想这样做),或者将其作为参数传递给filterVideos函数,以便在其中使用它。

Since you can't change the parameters that filter passes to its callback, the most obvious solution is to create a closure which defines the parameter and returns a function that has "list" in scope. 由于您无法更改过滤器传递给其回调的参数,因此,最明显的解决方案是创建一个闭包,该闭包定义该参数并返回范围为“ list”的函数。

var filterVideos = function(list) {
    return function(video) {
        return video.listId == list.id;
    }
};
var mapVideos = function(video){
    return {id: video.id, title: video.title};
};

return lists.map(function(list) {
    return {
      name: list.name,
      videos: videos.filter(filterVideos(list)).map(mapVideos)
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM