locked
Variable undefined in namespace

    Question

  • I have come across this issue a few times and think it is to do with a lack of understanding of deeper javascript on my part. When I set a variable on one page equal to the value of another variable and make it available through a WinJS.Namespace I cannot see the variable through that Namespace, it just reads as "undefined". 

    I have a WinJS.namespace defined:


    var testVar;

    function eg(){

    var example = 5;
    testVar = example;

    }

    WinJS.Namespace.define("Data", {

    testVar: testVar, });

    Then when I access testVar on another page, is comes up as undefined. I can see why this is (because it is set to the value of "example" which is not defined) so how can I overcome this?

    Thank you

    Monday, April 28, 2014 12:18 PM

Answers

  • You must be setting testVar after WinJS.Namespace.define() executes. To make this work, you could either (a) call eg() prior to the execution of WinJS.Namespace.define(), or (b) create a function to grab testVar, which is shown here, and use that when you want to get testVar:

        function getTestVar() {
            return testVar;
        }

        WinJS.Namespace.define("Data", {
            testVar: testVar,
            getTestVar: getTestVar
        });

    • Marked as answer by r.c.byrne Thursday, May 01, 2014 12:28 AM
    Tuesday, April 29, 2014 9:58 PM

All replies

  • Hi.

    Did you call eg function before accessing testVar?

    If you don't set any value to testVar, compiler know nothing about this var.

    (Initialize testVar o call eg before accesing).

    Hope this help you.

    Monday, April 28, 2014 3:53 PM
  • Do you access it via Data.testVar ? Also you need to call eg() to give it some data before access it.
    Monday, April 28, 2014 4:12 PM
  • To add to what these guys are saying, you should make sure the scope of the namespace encompasses your other page as well. One way to make sure all your pages can access it is declaring the name space in the default.js file. This will ensure that they can access it if your pages are hosted in the default.html.
    Monday, April 28, 2014 4:24 PM
  • Hello, This code was very shortened down. Yes I have set testVar before trying to access it and I have called "Data.testVar" from my other page.

    I can definitely see the namespace from my other page because I use it for a lot of things, and in visual studio if I make a break point and hover over "Data" I can see all the variables and methods that are available to me but testVar displays as "undefined"

    I think it is because "testVar" is pointing to "example" so when I call the function from another page it cant see "example"

    • Edited by r.c.byrne Tuesday, April 29, 2014 10:48 AM
    Tuesday, April 29, 2014 10:41 AM
  • You must be setting testVar after WinJS.Namespace.define() executes. To make this work, you could either (a) call eg() prior to the execution of WinJS.Namespace.define(), or (b) create a function to grab testVar, which is shown here, and use that when you want to get testVar:

        function getTestVar() {
            return testVar;
        }

        WinJS.Namespace.define("Data", {
            testVar: testVar,
            getTestVar: getTestVar
        });

    • Marked as answer by r.c.byrne Thursday, May 01, 2014 12:28 AM
    Tuesday, April 29, 2014 9:58 PM
  • The reason you are getting undefined value for "testVar" is because the "example" value of 5 is not assigned to "testVar" when you made the call on "Data" namespace.  In your code, undefined just means there's nothing there.  Make sure that "testVar" has a value of "example" first if you want it get its value when you call it from the namespace.

    var testVar; //call eg function first

    //so that testVar will have some value on it

    //before it gets called in the Data namespace eg(); ///// function eg(){ var example = 5; testVar = example; } WinJS.Namespace.define("Data", { testVar: testVar, });




    Wednesday, April 30, 2014 4:44 AM
  • Thank you! Worked perfectly
    Thursday, May 01, 2014 12:29 AM