RobG said:
Thomas said:
[...]
[...]
I think in both cases their values are references because, to me, it
is less confusing. As I posted eariler, given:
var a = 5,
b = a;
You can think of a and b as refering to the same instance of 5.
But you should not. While Smalltalk undoubtedly is an ancestor of
JavaScript/ECMAScript, by contrast `5' is _not_ an object in ECMAScript
implementations (it is only being implicitly converted to a Number
instance sometimes).
So, in your words, `b' "refers" to _another_ `5' than `a'.
No, in my words, 'b' and 'a' refer to the same '5'. Since that '5'
can't be modified, the value of 'b' can only be changed by assigning a
new value, even if it's another '5'.
That's nonsense. Have you subscribed to VK101 by now?
There is no imperative to copy the value to b. The value '5' can't be
modified, so there is no chance that assigning a new value to a will
result in a change of the value of b. It seems an implementation may,
in the interests of efficiency, make a and b the same primitive.
I tried Jorge's VB test in IE and Firefox (code below). One function
created 1,000 global variables with the same 375KB of text assigned to
each. A second function assigned a unique string to each by slightly
modifying its original string. Results were:
In IE 6:
Start: 609MB
Load page in new instance of IE: 617MB
Create 1,000 variables: 1,367MB
Modify them: 1,367MB
In Firefox 3.6
Start: 612MB
Load page in new tab: 615MB
Create 1,000 variables: 617MB
Modify them: 1,380MB
I think the above shows that IE creates 1,000 copies if the same
string initially, consuming about an extra 650MB of memory. Assigning
new values of the same size doesn't use any extra memory. However,
Firefox's much lower initial memory use indicates that it creates
1,000 references to the same string. It is only when a new string is
assigned to each variable that it creates 1,000 separate strings and
consumes and extra 760MB or so of memory.
No, implementation details are at the core of the issue.
So if implementation details matter (and you seem to think they do),
then you can argue on a per implementation basis as to whether my
original example above (var a=5, b=a; etc.) results in a reference to
the same primitive or a new copy being created.
I don't think implementation details matter, my opinion is purely a
theoretical exercise to create a system for understanding behaviour
that is consistent with the specification and observation. My idea of
"references" to primitives seems to fit with Firefox, your idea of
each assignment creating a new copy, always, fits with IE.
I might have a Message-ID.
I would appreciate it if you can find it.
Test code:
<script type="text/javascript">
// Text seed
var txt = "..."; // Slab of text, say 3,500 characters
// bigTxt will be about 100 times bigger than txt
var bigTxt = (function() {
for (var i=100, s=''; i; i--) {
s += txt;
}
return s;
})();
// Create a bunch of global vars, assign bigTxt
function createVars() {
for (var i=1000; i; i--) {
window['a' + i] = bigTxt;
}
};
// Modify created globals
function modifyVars() {
for (var i=1000, t; i; i--) {
t = window['a' + i]
window['a' + i] = i + ' ' + t;
}
}
</script>
<button onclick="alert(a1);">Show a1</button>
<button onclick="createVars()">Create vars</button>
<button onclick="modifyVars()">Modify vars</button>