Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 2 of 2

This is a discussion on Scoping issue within the Plugins Development section, part of the Chrome Plugins category: Hi, Somehow I am having troubles understanding the scoping problem in the code below. I assumed an anonymous function would ...


  1. #1
    fbern is offline Junior Member
    Join Date
    Feb 2011
    Posts
    1

    Default Scoping issue

    Hi,

    Somehow I am having troubles understanding the scoping problem in the code below. I assumed an anonymous function would capture dynamically the scoping of its surrounding vars. From what I read on Javascript this should work.
    As I am not a Javascript expert, in fact, its my first use of the language, I might as well have applied the concept wrong.

    In the example below I would expect isWincognito and isTabcognito to point to the values determined by the callback functions.

    Any help is greatly appreciated!

    Code:
    	function isIncognitoMode() {
    		// it seems that the scope of is(Win|Tab)cognito 
    		// not captured properly when accessed in anonymous function
    
    		var isWincognito = false;
    		var isTabcognito = false;
    
    		// get win & tag incognito states
    		chrome.windows.getCurrent(function(win) {
    			isWincognito = win.incognito;
    		});
    		chrome.tabs.getCurrent(function(tab) {
    			isTabcognito = tab.incognito;
    		});
    
                    // somehow is(Win|Tab)cognito do not point to the values 
                    // determined by the callback functions.
    		//console.log(this.isWincognito);
    		return isTabcognito || isWincognito;
    	}
    Thanks!

    Fabio

  2. #2
    devnull69 is offline Member
    Join Date
    Jul 2010
    Posts
    32

    Default

    The problem is not the scope ... it's a racing condition frequently happening with (asynchronous) callbacks.

    1. You set isWincognito and isTabcognito to false
    2. You initiate two asynchronous functions ... the program flow doesn't wait for the callbacks to end
    3. You return false
    4. "Some time after" the return the callbacks are being executed and change isWincognito or isTabcognito in the correct scope (the closure) ... but the return is "long" done

Similar Threads

  1. chrome issue
    By saxxoo in forum Chrome Troubleshooting
    Replies: 0
    Last Post: 01-19-2011, 02:34 PM
  2. Chrome/Win 7 issue
    By bg26892 in forum Chrome Troubleshooting
    Replies: 0
    Last Post: 08-17-2010, 02:43 AM
  3. hotlinking issue
    By ChaosKiller in forum Plugins Development
    Replies: 2
    Last Post: 01-10-2010, 03:53 AM
  4. cookies secure issue
    By mdr in forum Bugs and Vulnerabilities
    Replies: 0
    Last Post: 11-25-2009, 08:05 AM
  5. ext. TAB issue
    By steelbender in forum Plugins Troubleshooting
    Replies: 1
    Last Post: 10-09-2009, 01:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •