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


Groups > comp.lang.javascript > #32652 > unrolled thread

Associating this with that

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2017-02-24 16:38 +1100
Last post2017-02-24 08:10 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Associating this with that Andrew Poulos <ap_prog@hotmail.com> - 2017-02-24 16:38 +1100
    Re: Associating this with that Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-24 12:59 +0000
      Re: Associating this with that Andrew Poulos <ap_prog@hotmail.com> - 2017-02-25 16:33 +1100
    Re: Associating this with that "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-02-24 08:10 -0800

#32652 — Associating this with that

FromAndrew Poulos <ap_prog@hotmail.com>
Date2017-02-24 16:38 +1100
SubjectAssociating this with that
Message-ID<EeSdnRDlAqBUWzLFnZ2dnUU7-VvNnZ2d@westnet.com.au>
If I have code like this:

var Con = function(a) {
    this.blah = a;
    collect.push(this.blah);
};

var collect = [],
     foo = new Con(15),
     bar = new Con(52),
     bao = new Con(22),
     boo = new Con(54);

How can I know which object the this values stored in "collect" refer to 
when within one of the objects? My problem is that  change to a value 
in, say, foo.blah will change the values for all the other blahs as well 
as some other related properties.

Should "collect" just store the various this values instead?

Andrew Poulos

[toc] | [next] | [standalone]


#32653

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-02-24 12:59 +0000
Message-ID<87shn34uei.fsf@bsb.me.uk>
In reply to#32652
Andrew Poulos <ap_prog@hotmail.com> writes:

> If I have code like this:

Much better to post actual code.

> var Con = function(a) {
>    this.blah = a;
>    collect.push(this.blah);
> };
>
> var collect = [],
>     foo = new Con(15),
>     bar = new Con(52),
>     bao = new Con(22),
>     boo = new Con(54);
>
> How can I know which object the this values stored in "collect" refer
> to when within one of the objects?

I don't know what you mean.  collect holds numbers.  The code would work
the same if you write collect.push(a) instead of collect.push(this.blah).

> My problem is that  change to a
> value in, say, foo.blah will change the values for all the other blahs
> as well as some other related properties.

Really?  Can you post an example based on the code above?  I don't see
how it can match your description.

> Should "collect" just store the various this values instead?

Impossible to say since I don't know what this code is supposed to do.
What's the problem you are solving?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#32655

FromAndrew Poulos <ap_prog@hotmail.com>
Date2017-02-25 16:33 +1100
Message-ID<jpSdnS3nP5O6iizFnZ2dnUU7-WvNnZ2d@westnet.com.au>
In reply to#32653
On 24/02/2017 11:59 PM, Ben Bacarisse wrote:
> Andrew Poulos <ap_prog@hotmail.com> writes:
>
>> If I have code like this:
>
> Much better to post actual code.
>
>> var Con = function(a) {
>>    this.blah = a;
>>    collect.push(this.blah);
>> };
>>
>> var collect = [],
>>     foo = new Con(15),
>>     bar = new Con(52),
>>     bao = new Con(22),
>>     boo = new Con(54);
>>
>> How can I know which object the this values stored in "collect" refer
>> to when within one of the objects?
>
> I don't know what you mean.  collect holds numbers.  The code would work
> the same if you write collect.push(a) instead of collect.push(this.blah).

Yes, you're right. I've misunderstood what 'collect' was storing.

>> My problem is that  change to a
>> value in, say, foo.blah will change the values for all the other blahs
>> as well as some other related properties.
>
> Really?  Can you post an example based on the code above?  I don't see
> how it can match your description.
>
>> Should "collect" just store the various this values instead?
>
> Impossible to say since I don't know what this code is supposed to do.
> What's the problem you are solving?

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#32654

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-02-24 08:10 -0800
Message-ID<8d606737-29cf-4232-9c5c-9c9e16a76c5c@googlegroups.com>
In reply to#32652
On Thursday, February 23, 2017 at 11:38:26 PM UTC-6, Andrew Poulos wrote:
> If I have code like this:
> 
> var Con = function(a) {
>     this.blah = a;
>     collect.push(this.blah);
> };
> 
> var collect = [],
>      foo = new Con(15),
>      bar = new Con(52),
>      bao = new Con(22),
>      boo = new Con(54);
> 
> How can I know which object the this values stored in "collect" refer to 
> when within one of the objects? My problem is that  change to a value 
> in, say, foo.blah will change the values for all the other blahs as well 
> as some other related properties.
> 
> Should "collect" just store the various this values instead?


Why not something like this?

function Con(value){
    if(!(this instanceof Con))
        return new Con(value)
    this.value = value
}

var collect = []

var vals = [15,52,22,54].map(Con)

vals.forEach(function(con){ collect.push(con) })

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web