Note that there are some explanatory texts on larger screens.

plurals
  1. POLosing object reference on lookup in Javascript
    primarykey
    data
    text
    <p>I'm working on an extension for Google Chrome and I ran into the following situation:</p> <p>I'm trying to get all the existing tabs from all the opened windows in the same instance of Google Chrome. I manage to get them and construct an array of objects that contain the relevant data for me.</p> <p>When I look at the constructed array using console.log (which is saved for future use also) I can see the collection of objects, but I can't reference them (when I try I get undefined).</p> <p>I tried to save the array outside my object in a container, but nothing changes.</p> <p>Any idea why the reference to the objects go away when I try to look them up? Thanks.</p> <p>Here is the code:</p> <pre><code>(function(window){ //defining a namespace var example = { bmarksmaster: (function() { var bmarksmaster = function() { return new bmarksmaster.fn.init(); } bmarksmaster.fn = bmarksmaster.prototype = { debug: false, tabs: [], constructor: bmarksmaster, init: function() { return this; }, windowParser: function(ctx, filter) { var local = ctx; var filter = filter; return function(wObj) { if((wObj !== null) &amp;&amp; (wObj !== undefined)) { for(var idx in wObj) { var cw = wObj[idx]; if((cw.tabs !== null) &amp;&amp; (cw.tabs !== undefined)) { var cwtabs = cw.tabs; for(var tabIdx in cwtabs) { local.tabs.push(filter(tabIdx, cwtabs[tabIdx])); } } } } }; }, getTabs: function() { var returnData = []; chrome.windows.getAll( { "populate": true }, this.windowParser(this, function(i, e) { var data = {}; if(!e.incognito) { data["title"] = e.title; data['url'] = e.url; data['favicon'] = e.favIconUrl || ""; } return data; })); return this.tabs; }, getTab: function(callback) { this.getTabs(); for (var tabIdx in this.tabs) { if(callback(tabIdx, this.tabs[tabIdx])) { return this.tabs[tabIdx]; } } }, getTabsData: function(callback) { var data = []; var tabs = []; tabs = this.getTabs(); console.log(this.tabs[0]); for (var tabIdx in tabs) { console.log(tabs[tabIdx]); var tabData = callback(tabIdx, tabs[tabIdx]); if(tabData) { data.push(tabData); } } return data; }, setDebug: function() { this.debug = true; }, resetDebug: function() { this.debug = false; } }; bmarksmaster.fn.init.prototype = bmarksmaster.fn; return bmarksmaster; })() }; window.example = example; })(window); //end of bmarksmaster.js file console.log(example.bmarksmaster().getTabs()); //this works, I can see the array console.log(example.bmarksmaster().getTabs()[0]); //this doesn't work, I get undefined, never mind the shortcut </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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