tags:

views:

67

answers:

7

Hi,

I have the following setup:

var Dash = {
    nextIndex: 0,
    dashboards: <some value>,

    display: function()
    {
        var dashboard = Dash.dashboards[Dash.nextIndex];
        document.getElementById("iframe3").src = dashboard.url;
        Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
        setTimeout(Dash.display, dashboard.time * 1000);
    }
};

With this setup, is it possible to set programmatically, the value of "some value" prior to this declaration?

In addition, is it possible to have be a javascript function call that retunrs a value to dashboards?

Thanks.

A: 

How about this?

dashboards: null,
Muneer
Did you try this one?
Muneer
A: 

To set some value to dashboards

Dash.dashboards = "something"
Codler
A: 

As dashboards is going to hold an array I would use an empty array: dashbords: []

cmaderthaner
+1  A: 

Not prior (short of dummy values like null) but you can do so afterwards using Dash.dashboards= some-value;

+1  A: 

No.

You would have to define Dash in order to define Dash.dashboards, and as soon as your listed code runs it is all overwritten.

So, no.

Sky Sanders
+1  A: 

Javascript has lexical scoping and not block scoping . What that means is that all variable declaration in javascript are shifted to the top of the function .

So the code that you wrote is internally changed to something like :

    var Dash = undefined ;
   // other lines of codes 
    Dash = {
        nextIndex: 0,
        dashboards: some_value,

        display: function()
        {
            var dashboard = Dash.dashboards[Dash.nextIndex];
            document.getElementById("iframe3").src = dashboard.url;
            Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
            setTimeout(Dash.display, dashboard.time * 1000);
        }
    };

So you basically want to access the value of Dash.dashboard before Dash is assigned, which would fail as Dash is undefined . and undefined.dashboard would give you an error . However you could definitely call a function in the assignment operation that populates some_value

var Dash = {
        dashboards: function_call()
}

Btw , why exactly would you want to use Dash.dashboard before the assignment operation ? If you could let us know that , there might be a better way of doing it .

NM
+1  A: 

You can not . Dash would be undefined until it is assigned . so Dash.dashboard would not be accesible .

jmhowwala