繁体   English   中英

JavaScript函数内的函数-需要了解以下代码:

[英]Function within function in JavaScript - Need to understand this code:

我在名为render的函数中包含以下代码。 如何在render函数之外调用str变量值?

另外,请您说明以下代码? 我对js来说还很陌生,而head在查看以函数作为参数的函数调用时感到很痛苦。

我的理解是app.getList是一个将函数作为参数的对象? 但它没有返回任何东西。 抱歉,我在这里迷路了。

app.getList("FieldList", function(reply){
    var str = "";
    $.each(reply.qFieldList.qItems, function(index, value) {
        str +=  value.qName + ' ';
    });
    console.log(str);
});

完整代码:

 define(["jquery", //mashup and extension interface "qlik", //add stylesheet "text!./css/mystyle.css", "client.utils/state", "client.utils/routing" ], function($, qlik, cssContent, clientState, clientRedirect) { /*-----------------------------------------------------------------*/ // function redirect (sheetId){ // clientRedirect.goToSheet(sheetId, Object.keys(clientState.States)[clientState.state]) // } /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ var render = function($elem, layout) { var html = '', app = qlik.currApp(); //get list of tab objects and insert into div app.getAppObjectList('sheet', function(arrayitem) { //for each sheet in the app, create a list item $.each(arrayitem.qAppObjectList.qItems, function(myindex, myvalue) { //include the sheet id as the list item id to be used as a reference for active sheet html += '<li id="' + myvalue.qInfo.qId + '">'; // onClick="redirect(' + value.qInfo.qId + '); //wrap anchor tag to be used by bootstrap styling html += '<a>'; //give the link the same name as the sheet html += myvalue.qData.title; html += '</a>'; html += '</li>'; }); html += '</ul></div>'; html += "<button id='myButton'> Click Me!! </button>"; console.log(arrayitem.qAppObjectList); console.log(html); //insert html into the extension object return $elem.html(html); }); /* Test Code Start from here */ app.getList("FieldList", function(reply) { var str = ""; $.each(reply.qFieldList.qItems, function(key, value) { str += value.qName + ' '; }); console.log(str); }); }; /*-----------------------------------------------------------------*/ return { /*-----------------------------------------------------------------*/ paint: function($element, layout) { console.count(); /*-----------------------------------------------------------------*/ $(function() { $element.html("#myButton").click(function() { // for(var mynum = 1; mynum <= 5; mynum++){ // alert('button test' + mynum); // }; }); }); /*-----------------------------------------------------------------*/ render($element, layout); /*-----------------------------------------------------------------*/ } }; }); 

app.getList可能是异步的 (意味着它在后台运行)。 您传递给它的函数是一个回调 一旦完成AJAX调用(或运行了任何异步方法),该函数将在将来的某个时间运行。

你的回调被传递reply ,这是从“回报”值getList() 不能从此函数外部访问str 您只需要在该函数中使用reply和/或str做任何代码。

暂无
暂无

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

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