oops – incorrectly posted
apologize to all. misread my code and misinterpreted its behavior.
source code
have page in which variable shoe is initialized twice, once in index.html and once in an included script.js:
index.html
<script src='script.js'></script>
<script>
var shoe = { color: 'pink' };
</script>
script.js
var shoe = { size: 13 };
resulting value
in chrome, the resulting value of shoe was
>> shoe = { color: 'pink', size: 13 }
it appears that chrome concatenated the two declarations – which is super cool
and super polite.
it this standard behavior? can I count on this between browsers?
runtime example
see http://www.trailsandtribulations.net/tech/barebone.html
the sources are:
- http://www.trailsandtribulations.net/tech/barebone.jade
- http://www.trailsandtribulations.net/scripts/session.js
(I have temporarily unbundled the javascript)
the variable that’s declare more than once is state
The situation in your question is actually not occurring at all (at least with version 23 of chrome). I’ll attempt to show you this is the case.
First, ‘var state’ is declared in your ‘session.js’ with the follow attributes.
Then within the head of your html you have a script block that declares ‘var state’ again, this overwrites the original object as it should.
After this, there are many places in your code where you are assigning values to your state object. This is why it starts to look like both objects were merged. One give away that it wasn’t is that ‘state.inited’ does not exist, yet it is declared with this property in the ‘session.js’ script. Your object is getting these properties via other functions. My advice would be to debug through your JS at different stages/events and check ‘state’ object to work out where these values are getting added. Remembering that ‘state.foo = 1;’ will give the ‘state’ object a property of ‘foo’ regardless of it being declared with it or not.
Hope that helps.