1

In the below code, I want to know what would be the value of tabId inside doStuffWithReport function. Would it be the same value of tabId that was sent while calling sendMessage or it might change during the course of period?

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    // we suppose that tabId is 5.
    function doStuffWithReport(report) {
       // what would be the value of tabId? still 5? or it might change to
       // something else if, in the meantime, we received another 
       // call for chrome.tabs.onUpdated?
    }
    // we are sending a message to tabId 5.
    chrome.tabs.sendMessage(tabId, {
        text: 'report_me'
    }, doStuffWithReport);
});

Edit: I tested it myself. It remains the same, i.e., tabId would remain 5 even if there is another call on chrome.tab.onUpdated.

0

1 Answer 1

1

Well, first let's simplify the code to look at the underlying behavior:

function outside(){
  var out = 5;
  function inside(){
    console.log(out);
  }
  inside();
}

In this example it will obviously print out 5 when outside is called, but since inside is only defined locally, it will be undefined unless called from within outside.

To make it a bit more complex, let's not make out static and have it based on a parameter:

function outside(out){
  function inside(){
    console.log(out);
  }
  inside();
}

Now it is pretty much the same as before and will print out whatever we use as a parameter right away. Let's make it a bit more async by having inside be called after a random amount of time of up to a second and compare the then current value of out with the value of out that is printed at the time inside is called while calling it several times consecutively.

function outside(out){
  function inside(){
    var dt = Date.now();
    console.log(dt + ' : ' +out);
  }
  var n = Math.floor(Math.random() * 1000);
  var d = Date.now()+n;
  console.log(d + ' : ' + out);
  setTimeout(inside,n);
}
for(var x=0;x<25;x++){outside(x);}

From the output of this, we can see that the value of out that inside has at the time of being called is the same value as when we set the timeout for inside to be called.

From this we can say that tabId would indeed still be 5 when doStuffWithReport is called.

Sign up to request clarification or add additional context in comments.

1 Comment

great answer! I checked that myself too..it would remain the same.

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.