简体   繁体   English

将回调函数和参数传递给javascript函数

[英]passing callback function and parameters to a javascript function

I'm pretty new to JS. 我是JS的新手。 I have a helper class that does some of the common fetching to the server. 我有一个帮助程序类,它执行一些常见的服务器提取。 Currently, one of those functions would look like this: 目前,其中一个功能如下所示:

getAvailablePlatforms: function (platform) {
            var self = this;
            if (_.isUndefined(platform) || platform == '') {
                platform = "crystalproject";
            }

            $.ajax({
                url: '/api/platform/' + platform,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    console.log("Got data");
                    self.platforms = data;
                    self.eventAggregator.trigger('getplatforms');  
                },
            });
        }

So what I wanted to try to make this more flexible was to pass in the callback function. 所以我想尝试使其更灵活的是传入回调函数。 So I tried this: 所以我尝试了这个:

getAvailablePlatforms: function (platform, callback) {
            var self = this;
            if (_.isUndefined(platform) || platform == '') {
                platform = "crystalproject";
            }

            $.ajax({
                url: '/api/platform/' + platform,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    console.log("Got data");
                    self.platforms = data;
                    self.eventAggregator.trigger('getplatforms');  
                    if (callback && typeof(callback) === "function") {
                        callback(data);
                    }
                },
            });
        }

How I want to call it though, is like this: 我怎么称呼它,就像这样:

var helper = new Helper();
helper.getAvailablePlatforms('', this.populatePlatforms(???????));

I want my callback function to use the data that it received in the callback. 我希望我的回调函数使用它在回调中收到的数据。 How can I achieve that? 我怎样才能做到这一点? Thanks in advanced! 提前致谢!

You would just pass in the function reference. 你只需要传入函数引用。 You don't want to call the function by ending it will () while passing the callback, instead of the function reference the call back would end up being the result of the function (if nothing is returned it will be undefined). 你不想通过在传递回调时结束它()来调用函数,而不是函数引用,回调最终会成为函数的结果(如果没有返回它将是未定义的)。 populatePlatforms inside getAvailablePlatforms: function (platform, callback) { getAvailablePlatforms: function (platform, callback) { populatePlatforms getAvailablePlatforms: function (platform, callback) {

So you would want to go with: 所以你想要:

helper.getAvailablePlatforms('', this.populatePlatforms);

For passing in the context use ecmascript5 function.prototype.bind function. 对于在上下文中传递使用ecmascript5 function.prototype.bind函数。

helper.getAvailablePlatforms('', this.populatePlatforms.bind(this));

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

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