Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access an object from a callback function?
    primarykey
    data
    text
    <p>I feel bad because it is very likely this has already been answered and I am just not using the right search terms. I am very new to asynchronous JavaScript. So, I will lead with an apology. If someone could even just help me get the right search terms I would most appreciate it.</p> <p>I am trying to use the Google feeds api. I have it working when I follow the stock example as laid out in the hello world section. I am trying to make something that is a little more extensible so that I can use it in several places. So I created an object...</p> <pre><code>function AlertRSS(num, url, div, date) { this.num = typeof num !== 'undefined' ? num : 5; this.url = typeof url !== 'undefined' ? url : 'http://arstechnica.com/author/caseyjohnston/feed/'; this.sel = typeof sel !== 'undefined' ? sel : '#alert'; this.date = typeof date !== 'undefined' ? date : this.getYesterday(); } </code></pre> <p>I then try to call the object inside of the method...</p> <pre><code>AlertRSS.prototype.displayFeed = function() { var retval = null; var feed = new google.feeds.Feed(this.url); feed.load(function(result) { var tmp = this; if (!result.error) { for ( var i = 0; i &lt; result.feed.entries.length; i++) { var entry = result.feed.entries[i]; console.log(sel + ' &lt;h2&gt;&lt;a href="' + entry.link + '"&gt;' + entry.title + '&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;'); $(tmp.sel).append('&lt;h2&gt;&lt;a href="' + entry.link + '"&gt;' + entry.title + '&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;'); } } }); }; </code></pre> <p>However, it seems like I am not able to access the properties from the object. I keep getting a <code>Uncaught ReferenceError: sel is not defined</code> in the console. </p> <p>I think the issue is related to scope, but at this point I feel a little out of my depth. Any help would be most appreciated.</p> <h1>Update</h1> <p>For a first post this was a travesty. I had numerous mistakes in my code. Both responses were correct. However, in case another poor newb like me sees this question, I wanted to put working code out there. </p> <p>The issue turned out to be placement of the <code>var tmp = this;</code> line. It needs to be placed outside of the internal callback function to work. As per Tomasz Nurkiewicz suggestion, I also changed <code>var tmp</code> to <code>var that</code>. Working code is as follows:</p> <pre><code>function AlertRSS(num, url, sel, date) { this.num = typeof num !== 'undefined' ? num : 5; this.url = typeof url !== 'undefined' ? url : 'http://arstechnica.com/author/caseyjohnston/feed/'; this.sel = typeof sel !== 'undefined' ? sel : '#alert'; this.date = typeof date !== 'undefined' ? date : this.getYesterday(); } AlertRSS.prototype.displayFeed = function() { var feed = new google.feeds.Feed(this.url); var that = this; feed.load(function(result) { if (!result.error) { for ( var i = 0; i &lt; result.feed.entries.length; i++) { var entry = result.feed.entries[i]; console.log(that.sel + ' &lt;h2&gt;&lt;a href="' + entry.link + '"&gt;' + entry.title + '&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;'); $(that.sel).append('&lt;h2&gt;&lt;a href="' + entry.link + '"&gt;' + entry.title + '&lt;/a&gt;&lt;/h2&gt;&lt;br /&gt;'); } } }); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload