in Education by
I'm discovering the concept of "objects" in JavaScript. I'm making an RSS Parser, and I have an error (commented). function MyParser (feed_url) { // Construct "use strict"; this.feedUrl = feed_url; this.pubArray = []; if (typeof (this.init_ok) == 'undefined') { MyParser.prototype.parse = function () { "use strict"; var thisObj = this; $.get(this.feedUrl, function (data, textStatus, jqXHR) { if (textStatus == 'success') { var xml = jqXHR.responseXML, //lastBuildDate = new Date($(xml).find('lastBuildDate').text()); items = $(xml).find('item'); items.each(function () { var pubSingle = thisObj.makeObj($(this).find('pubDate').text(), $(this).find('link').text(), $(this).find('title').text(), $(this).find('description').text(), $(this).find('encoded').text(), $(this).find('commentRss').text(), $(this).find('comments').last().text()); thisObj.pubArray.push(pubSingle); }); console.log(thisObj.pubArray); // OK } }, 'xml'); console.log(this.pubArray); // Empty return (this.pubArray); }; MyParser.prototype.makeObj = function (pubDate, pubLink, pubTitle, pubDesc, pubContent, pubComCount, pubComLink) { "use strict"; var pubSingle = {}; pubSingle.pubDate = new Date(pubDate); pubSingle.pubLink = pubLink; pubSingle.pubTitle = pubTitle; pubSingle.pubDesc = pubDesc; pubSingle.pubContent = pubContent; pubSingle.pubComCount = pubComCount; pubSingle.pubComLink = pubComLink; return (pubSingle); }; } this.init_ok = true; } If you look at the console.log(), you'll see that the line // OK is outputting my array correctly. But later, when returning from $.get, my array is empty. Does anybody have an idea why, and how to correct that please? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is not a problem with variable-scope. The problem here is that you're working with asynchronous flow and you're not thinking correctly the flow. Let me explain: When you do your .get, you fire a parallel asynchronous process that will request information from the browser, but your main program's flow keeps going, so when you get to your "return" statement, your array has not been filled yet with the response from your get method. You should use your array from inside the get callback and not outside of it, since you can't guarantee that the array will have the information you need. Does it make any sense? Let me know! Further explanation According to your comments, you're still doing something like this: var results = MyParser(feed_url); //code that uses results.pubArray And you cannot do that. Even though you're setting your "pubArray" inside your .get callback, you're trying to use pubArray right after you called MyParser and that's before the .get callback is called. What you have to do, is call your next step on your program's logic from within the .get callback... that's the only way you can be sure that the pubArray is filled with proper data. I hope that makes it clearer.

Related questions

0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    What type of scope is present in JavaScript? (a) Lexical (b) Literal (c) Both Lexical and Literal ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which of the following regarding scope is true about JAVAScript? A) Variables that have a local scope are only visible ... in your program have to be declared as global variables...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    Which object serves as the global object at the top of the scope chain? (a) Hash (b) Property (c ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    I'm having some trouble recently. On a WebForm I declare a static object, like this : public static ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I'm having some trouble recently. On a WebForm I declare a static object, like this : public static ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I'm having some trouble recently. On a WebForm I declare a static object, like this : public static ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    What will be the initial value of an object reference which is defined as an instance variable?...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    I need to filter out any overlaps of start and end (moment.js) times of the following eg array of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to determine equality for two JavaScript objects? (78 answers) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    All browsers are split into different parts or object that can be accessed using JavaScript. These parts are known as Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    Which object is the main entry point to all client-side JavaScript features and APIs? (a) Standard (b ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
...