Javascript OOP return value from function

I have a javascript object defined as follows:

function SocialMiner() { var verbose=true; var profileArray=new Array(); var tabUrl; this.getTabUrl=function() { logToConsole("getTabUrl is called"); chrome.tabs.getSelected(null, function(tab) { tabUrl = tab.url; logToConsole(tabUrl); }); return tabUrl; } ` 

Then I call this function in SocialMiner ojbect as follows:

  var pageUrl=miner.getTabUrl(); miner.logToConsole(pageUrl); 

What is the reason that the first call to logToConsole successfully prints the URL, and the second is undefined. Am I not returning the same value from a function?

Update. This is how I defined logToConsole:

 function logToConsole(text) { if (verbose) console.log(text); } this.logToConsole=logToConsole; 
0
source share
2 answers

In the second example, you call logToConsole as if it were a miner object function, but it is not.

 miner.logToConsole 

Edit

In the comments for the github example, this should make the function logToConsole function of the SocialMiner object. However, I did not read the class carefully, so be careful about how it is intended to be used.

 this.logToConsole=function(text) { if (verbose) console.log(text); } 
+2
source

LogToConsole seems to be defined globally somewhere; In any case, he is not a member of our SocialMiner class. Try the following:

 var pageUrl=miner.getTabUrl(); logToConsole(pageUrl); 
0
source

All Articles