简体   繁体   English

访问外部函数参数而不传入内部函数

[英]Access outer function param without passing in inner function

I have a function which does an async call & calls an anonymous function on success.The data param of anonymous function is used to collect the response from the server but it is not available inside the inner function unless passed as a param. 我有一个函数,它执行异步调用并在成功时调用匿名函数。匿名函数的data参数用于从服务器收集响应,但它在内部函数内部不可用,除非作为参数传递。

callService('POST', getDataInfo, detailData, function (data) {
            formDisplayGrid('.accruedGrid', '.accruedTable', 'Total Accrued');//doesn't work.
            formDisplayGrid(data,'.accruedGrid', '.accruedTable', 'Total Accrued'); //works
           });

callService is just a function which uses jQuery ajax to place a call. callService只是一个使用jQuery ajax发出调用的函数。

function callService(method, url, data, success) {
        var ajaxObject = {
            url: url,
            method: method,
            dataType: 'json',
            success: success
        }
        if (method == 'POST') {
            ajaxObject['data'] = data;
        }
        jQuery.ajax(ajaxObject);
    }

formDisplayGrid function just iterates over dataset to form HTML table. formDisplayGrid函数只是迭代数据集以形成HTML表。

function formDisplayGrid(data, modalSelector, mainGridSelector, totalLabel) {    
    jQuery(modalSelector).modal();
    if (typeof data != 'undefined' && data.Code === 200) {
        var mainGrid = jQuery(mainGridSelector);
        var tbody = '';

        jQuery.each(data['Data']['category'], function (k, v) {
            //some code here.
            jQuery.each(v['subcat'], function (k, v) {
                //some code here.
            });    
        });    
                //some code here.
        mainGrid.find('tbody').html(tbody).fadeIn(1200);
    }
}

Is that this is happening because the anonymous function is being executed in the jQuery.ajax function and it should have been available if the anonymous function was directly executed inside the callService function? 这是否正在发生,因为匿名函数正在jQuery.ajax函数中执行,如果匿名函数直接在callService函数内执行,它应该可用吗?

这个概念基本上很简单,你的formDisplayGrid()函数是回调函数的外部函数,因此它不应该访问该回调函数中的私有变量。但是如果你想要第一种方法工作你将不得不定义函数在回调函数中。

Your formDisplayGrid function won't be able to access any variables from where it was called unless those variables are global or they are in a closure. 除非这些变量是全局的或者它们在闭包中,否则formDisplayGrid函数将无法访问调用它的任何变量。

EDIT: your function has to get its data argument from somewhere - otherwise how is it going to know what data is?. 编辑:你的函数必须从某个地方获取data参数 - 否则它将如何知道数据是什么? There is a good reason that only the second one works. 有一个很好的理由,只有第二个工作。

EDIT2: here is a solution with a closure: EDIT2:这是一个带闭包的解决方案:

callService('POST', getDataInfo, detailData, function (data) {
    function formDisplayGrid(modalSelector, mainGridSelector, totalLabel) {
        // you no longer need the data variable    
        jQuery(modalSelector).modal();
        if (typeof data != 'undefined' && data.Code === 200) {
            var mainGrid = jQuery(mainGridSelector);
            var tbody = '';

            jQuery.each(data['Data']['category'], function (k, v) {
            //some code here.
                jQuery.each(v['subcat'], function (k, v) {
                //some code here.
                });    
            });    
                //some code here.
            mainGrid.find('tbody').html(tbody).fadeIn(1200);
        }
    }
    formDisplayGrid('.accruedGrid', '.accruedTable', 'Total Accrued');//should work.
});

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

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