Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16168

Re: Who's eaten my apple and why?

From Gene Wirchenko <genew@ocis.net>
Newsgroups comp.lang.javascript
Subject Re: Who's eaten my apple and why?
Date 2012-09-26 19:38 -0700
Organization A noiseless patient Spider
Message-ID <kde7681jjln1ffu43grf11thd274pqkokf@4ax.com> (permalink)
References <37c94a7b-70f8-47f5-b5b8-2a1b3350de43@u19g2000yqo.googlegroups.com> <k403fb$o5g$1@speranza.aioe.org> <f097689vq8t2i4d344qhsse20dg9jephgj@4ax.com> <k40ct8$fmi$1@news.albasani.net>

Show all headers | View raw


On Thu, 27 Sep 2012 04:16:08 +0200, Stefan Weiss
<krewecherl@gmail.com> wrote:

>On 2012-09-27 02:59, Gene Wirchenko wrote:

[snip]

>No, Christoph has it right. The entries in 'options' never referred to

     I know he has it right.  I have run both sets of code.  The
question is why is it that way?  He posted a reference that he claimed
explains it, but it does not do so.

>'o'. They, and the 'o' variable, hold a reference to the same option
>object. If you assign something else to 'o' (like null), then the 'o'
>variable will contain something else, but that doesn't affect the object
>itself.

     I know.

>When you write 'options.add(o)', you're not just passing the 'value' and
>'text' properties, you're passing (a reference to) the entire object.

     Yes, I know.  My issue is why does the second .add() wipe out the
first when another element object is not created?

>Here's a simpler but similar example:

     But not similar enough!

     I have put the analogous lines from Christoph's function after
yours.

>  var arr = [];
>  var o = {foo: "bar"};

var sel = document.getElementById('mySel');
var o = document.createElement('option');
o.value="a";
o.text="apple";

>  arr.push(o);

sel.options.add(o);

o = document.createElement('option');

     Where is the equivalent of this last line in your code?  Why do
you not have to create a second object in your example, but not doing
so with OP's code is why it does not work?  I understand your code.  I
do not understand Christoph's.

>  o.foo = "qux";

o.value="ora";
o.text="orange";

>  arr.push(o);

sel.options.add(o);

>  o = null;
>
>  print(arr[0].foo);
>  print(arr[1].foo);
>
>This will print "qux" twice. Even though the .foo property was "bar"
>when we added the first element, and even though we nulled the 'o'
>variable later.

     So what goes wrong with an element object?

>You can still modify an object after adding it to an array, or passing
>it to a method - as long as you have a reference to it. When we set 'o'
>to null, we lose that reference, but not the object.

     I know.  Now, why can we not reuse the object?

     And here I thought that I understood JavaScript fairly well, but
here we have something where it is not just that I do not understand,
but I misunderstand.

Sincerely,

Gene Wirchenko

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Who's eaten my apple and why? justaguy <lichunshen84@gmail.com> - 2012-09-26 14:43 -0700
  Re: Who's eaten my apple and why? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 01:35 +0200
    Re: Who's eaten my apple and why? justaguy <lichunshen84@gmail.com> - 2012-09-26 17:46 -0700
      Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 03:51 +0200
        Re: Who's eaten my apple and why? justaguy <lichunshen84@gmail.com> - 2012-09-26 19:44 -0700
          Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 22:06 +0200
    Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-26 17:59 -0700
      Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 04:16 +0200
        Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-26 19:38 -0700
          Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 05:44 +0200
            Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 09:28 -0700
              Re: Who's eaten my apple and why? Tim Streater <timstreater@greenbee.net> - 2012-09-27 17:40 +0100
                Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 16:24 -0700
              Re: Who's eaten my apple and why? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-29 20:47 +0100
          Re: Who's eaten my apple and why? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 13:00 +0200
            Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 09:34 -0700
              Re: Who's eaten my apple and why? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 19:14 +0200
                Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 21:40 +0200
                Re: Who's eaten my apple and why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-28 15:39 +0200
                Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 16:25 -0700
      Re: Who's eaten my apple and why? Tim Streater <timstreater@greenbee.net> - 2012-09-27 08:52 +0100
  Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-26 17:50 -0700

csiph-web