0

I have javascript object defined like this:

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 on SocialMiner ojbect like this:

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

What is the reason that first call to logToConsole successfully prints the Url, while second one says undefined. Am I not returning the same value from the function ?

Update: This is how I have defined logToConsole:

function logToConsole(text) 
    {
        if (verbose)
            console.log(text);
    }

    this.logToConsole=logToConsole;
4
  • Im missing something here; you don't seem to have defined a logToConsole method on your SocialMiner object. You've made a getTabUrl method, but no logToConsole. Commented Jun 2, 2011 at 20:42
  • @Tejs: Please check out the update I have made. Commented Jun 2, 2011 at 20:44
  • Your other question got closed as a duplicate, so I'm not sure if you saw the edit to my answer (bottom half of this answer), but I think the issue is actually timing of setting/getting because of the asynch nature of your calls... Commented Jun 8, 2011 at 3:57
  • You are right, the issue was solved. Sorry for marking it late as answer. Commented Jun 8, 2011 at 4:09

2 Answers 2

2

In the second example, you are calling logToConsole as if it is a function of the miner object, which is is not.

miner.logToConsole

Edit

Per comments about github example, this should make the logToConsole function par of the SocialMiner object. However, I didn't read the class thoroughly, so proceed with caution with regards to how it is intended to be used.

this.logToConsole=function(text) 
{
    if (verbose)
        console.log(text);
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Madhur, your update still doesn't have logToConsole as a method of SocialMiner. You're calling it as if it's a method of SocialMiner, but it's not.
logToConsole is not being attached to your SocialMiner still. You would need to do SocialMiner.prototype.logToConsole = function() { } to attach it to your objects.
@Madhur: It still looks like the function is defined outside of the miner class.
@Madhur, the SocialMiner function does not define logToConsole as part of the class. It appears to be an inline function to be called when SocialMiner is constructed. If you want to be able to use it as part of the Miner object, then you should probably construct it as such. I will edit my post as an example.
0

It appears that logToConsole is defined somewhere globally; in any case, it is not a member of our SocialMiner class. Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.