Explict Memory Managment
Brendan Eich
brendan at mozilla.com
Fri May 22 14:13:14 PDT 2009
On May 22, 2009, at 2:06 PM, Brendan Eich wrote:
> On May 22, 2009, at 1:53 PM, David Semeria wrote:
>
>>> First, null is not a "value" in the sense of object value. An object
>>> takes up arbitrary space associating property names with values.
>>> You're possibly confusing reference and referent again.
>>
>> That's very likely - I have only a very rudimentary idea of how the
>> language is implemented in the browser.
>
> It's not only an implementation issue. The language has reference
> types -- objects -- and mutation. Sometimes you must know about how
> more than one reference to an object being modified can affect the
> different reference holders, even if you are a language user only
> and not a language implementor.
The simplest case:
var obj = {hi: "there"};
var frobj = obj; // did we copy obj?
frobj.hi = "bye";
alert(obj.hi); // no, we copied the reference
Notice that string is apparently a value type. You can't mutate
primitive strings, so implementations can and do use references to
shared immutable strings under the hood (even to shared mutables, with
reference-counting or other tricks to avoid exposing the optimization)
under the hood. Of coure number, boolean, and the hidden types of null
and undefined are value types.
Because object is a reference type, people sometimes want something like
function copy(obj) {
var clone = {};
for (var i in obj)
clone[i] = obj[i];
return clone;
}
This assumes obj is an Object instance, without getters or setters,
etc. See the Object.extend thread for more:
https://mail.mozilla.org/pipermail/es-discuss/2008-July/006709.html
and jresig's super-duper implementation at
http://ejohn.org/files/object-extend.js
This could use ES5's Object meta-programming methods instead of
__lookupProperty__, but then it wouldn't work in extant browsers.
/be
More information about the es-discuss
mailing list