Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.karotte.org!uucp.gnuu.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1574086.9R1ReOYYeM@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 28 Sep 2012 15:39:17 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 47 NNTP-Posting-Date: 28 Sep 2012 15:39:18 CEST NNTP-Posting-Host: 4759b2c8.newsspool1.arcor-online.net X-Trace: DXC=eWFUbjhbF6V1`E>oC;JXEZic==]BZ:af^4Fo<]lROoRQnkgeX?EC@@PKPeR?^YhTLWDZm8W4\YJN\R=i2=[N6Y6ZgYTh I was talking about reference types with a co-worker a couple of months > ago, and we found an analogy that seemed to help: > > One can imagine variables as small envelopes, just large enough to hold > a single piece of paper. Some things can fit on this piece of paper > entirely, like numbers and strings, others are too large, like objects. > > When you create an object, it's like handing your cloak (the object) to > a checkroom attendant. He will put the cloak on a rack and hand you a > paper receipt to identify it later. You put this piece of paper in your > envelope: your variable now contains a reference to an object. > > Now you can use this receipt to access your cloak anytime. You can copy > the receipt to another envelope and it will still work. You can show it > to other attendants (methods), who will copy it from you and can now > also access your cloak. > > When you empty your envelope, you lose the reference. The cloak still > exists, but you won't be able to find it again. After a while, the > checkroom attendant may (or may not) notice that this cloak has been > hanging around for weeks, and will throw it away (objects with a > reference count of zero may be garbage collected). > > The analogy is getting a little stretched now, but bear with me... > > When you write options.add(o), that's like showing your receipt to an > attendant and saying "take this cloak from wherever it is and hang it on > the tree outside" (the DOM tree). You can do that any number of times, > but in the end your cloak will be hanging on the tree only once. If you > put something else in your envelope, like a blank piece of paper (o = > null), the cloak on the tree is unaffected by this. You just don't have > an easy way to find it again. This is arguably the best analogy for this mechanism that I have read so far. And it is funny, too. +1 PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee