I have the following setup:
var Dash = {
nextIndex: 0,
dashboards: <some value>,
display: function()
{
var dashboard = Dash.dashboards[Dash.nextIndex];
doc开发者_如何学编程ument.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.
Not prior (short of dummy values like null) but you can do so afterwards using Dash.dashboards= some-value;
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.
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 .
You can not . Dash would be undefined until it is assigned . so Dash.dashboard would not be accesible .
How about this?
dashboards: null,
To set some value to dashboards
Dash.dashboards = "something"
As dashboards is going to hold an array I would use an empty array: dashbords: []
精彩评论