简体   繁体   English

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

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

I have below code within a function called render. 我在名为render的函数中包含以下代码。 How do I call the str variable value outside render function? 如何在render函数之外调用str变量值?

Also, please can you explain below code? 另外,请您说明以下代码? I'm fairly new to js and head is hurting looking at the function calls having functions as a parameter. 我对js来说还很陌生,而head在查看以函数作为参数的函数调用时感到很痛苦。

My understanding is that app.getList is an object which takes function as a parameter? 我的理解是app.getList是一个将函数作为参数的对象? but it is not returning anything. 但它没有返回任何东西。 Sorry, I'm lost here. 抱歉,我在这里迷路了。

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

Full Code: 完整代码:

 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 is probably asynchronous (meaning it runs in the background). app.getList可能是异步的 (意味着它在后台运行)。 The function you've passed to it is a callback . 您传递给它的函数是一个回调 That function will be ran at some point in the future, once the AJAX call (or whatever asynchronous method is ran) is done. 一旦完成AJAX调用(或运行了任何异步方法),该函数将在将来的某个时间运行。

Your callback is passed reply , which is the "return" value from getList() . 你的回调被传递reply ,这是从“回报”值getList() You cannot access str from outside of this function. 不能从此函数外部访问str You need to do whatever code with reply and/or str in that function only. 您只需要在该函数中使用reply和/或str做任何代码。

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

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