Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The solution is to save a reference to <code>this</code> within the constructor (available later on via closure):</p> <pre><code>var that = this; //in the top of the SocialMiner constructor function </code></pre> <p>and in <code>setTabUrlValue</code> use:</p> <pre><code>that.tabUrl=pageUrl; </code></pre> <p>I suspect running a method as a function (<code>callback</code>) loses scope, i.e. doesn't know of any <code>this</code> anymore. In other words, it runs within the scope of the constructor, not as a method of the instance using it. A variable referencing <code>this</code> in the constructor scope <em>is</em> available to the function, and <code>that</code> points to the right <code>this</code> on instance creation.</p> <p>You could also force <code>callback</code> to run in the current instance scope like this:</p> <pre><code>callback.call(this,tab.url); </code></pre> <p>In that case you can leave <code>this.tabUrl=pageUrl;</code> as it is.</p> <p>This is an simplification of your code. The methods return <code>this</code> to be able to directly reference a property of the instance (see <code>console.log</code> last line):</p> <pre><code>function Some(){ var that = this; // note: not used in this example this.getA = function(callback){ someval = 'foobar'; callback.call(this,someval); return this; }; this.getB = function(val){ this.val = val; return this; }; } var some = new Some; console.log( some.getA(some.getB).val ); //=&gt; foobar </code></pre> <p>Taking a look @ your code again, I think you're loosing scope twice, because <code>callback</code> is called from within another callback. That's why I think your code on that spot should be:</p> <pre><code>chrome.tabs.getSelected( null, function(tab) { callback.call(that,tab.url); //&lt; use that here } ); </code></pre> <p>Furthermore, in you code @ github, I don't see any instantiation of the <code>miner</code> instance.</p>
 

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