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


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

Reference var in (function(){})()

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2017-08-01 12:09 +1000
Last post2017-08-02 11:33 +0200
Articles 6 — 3 participants

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


Contents

  Reference var in (function(){})() Andrew Poulos <ap_prog@hotmail.com> - 2017-08-01 12:09 +1000
    Re: Reference var in (function(){})() "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-08-01 09:39 +0200
    Re: Reference var in (function(){})() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-08-01 12:40 +0200
      Re: Reference var in (function(){})() Andrew Poulos <ap_prog@hotmail.com> - 2017-08-02 14:06 +1000
        Re: Reference var in (function(){})() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-08-02 11:38 +0200
      Re: Reference var in (function(){})() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-08-02 11:33 +0200

#33676 — Reference var in (function(){})()

FromAndrew Poulos <ap_prog@hotmail.com>
Date2017-08-01 12:09 +1000
SubjectReference var in (function(){})()
Message-ID<KZ-dnfr95s94f-LEnZ2dnUU7-bnNnZ2d@westnet.com.au>
There's some code that looks like

var con = (function() {
   var opt0 = {},
       opt1 = {},
       opt2 = {},
       opt3 = {},
       ...

   function x() {
     //
   }
})()

Within the function x I need to reference each of the (many) "opt" 
objects. I'd like to reference them in a for loop e.g.

for (i = 0; i < len; i++) {
   eval("opt" + i) = ...
}

but how can I do it without using eval?

Andrew Poulos

[toc] | [next] | [standalone]


#33677

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-08-01 09:39 +0200
Message-ID<XnsA7C4622AC71A4eejj99@194.109.6.166>
In reply to#33676
Andrew Poulos <ap_prog@hotmail.com> wrote on 01 Aug 2017 in 
comp.lang.javascript:

> There's some code that looks like
> 
> var con = (function() {

The encapseling has nothing to do with your Q, it seems.

>    var opt0 = {},
>        opt1 = {},
>        opt2 = {},
>        opt3 = {},
>        ...
> 
>    function x() {
>      //
>    }
>})()
> 
> Within the function x I need to reference each of the (many) "opt" 
> objects. I'd like to reference them in a for loop e.g.
> 
> for (i = 0; i < len; i++) {
>    eval("opt" + i) = ...
>}
> 
> but how can I do it without using eval?

eval is evil!

Use an array for series of numbered variables.

'use strict';
let opt = [], i;
opt[0] = {};
opt[1] = {};
opt[2] = {};
opt[3] = {};
function x(ob) {
	for (i = 0; i < ob.length; i++)
		ob[i].blah = 'rrr-' + i;
};
x(opt);
alert('>>>> ' + opt[3].blah);


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#33678

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-08-01 12:40 +0200
Message-ID<3122783.iIbC2pHGDl@PointedEars.de>
In reply to#33676
Andrew Poulos wrote:

> There's some code that looks like
> 
> var con = (function() {
>    var opt0 = {},
>        opt1 = {},
>        opt2 = {},
>        opt3 = {},
>        ...

Who is doing such nonsense?
 
>    function x() {
>      //
>    }
> })()
> 
> Within the function x I need to reference each of the (many) "opt"
> objects.

Objects have identity, not name.

> I'd like to reference them in a for loop e.g.
> 
> for (i = 0; i < len; i++) {
>    eval("opt" + i) = ...
> }
> 
> but how can I do it without using eval?

You cannot.  Therefore, use an Array instance –

  var con = (function () {
    var opt = [
      {},
      {},
      {},
      {}
      // , ...
    ];
 
    function x ()
    {
      for (var i = 0, len = opt.length; i < len; ++i)
      {
        // opt[i] = ...
      }
    }

    // x(…)
  })();

– or an Object instance –

  var con = (function () {
    /* “opt” then refers to an object that does not inherit properties */
    var opt = Object.assign(Object.create(null), {
      0: {},
      1: {},
      2: {},
      3: {}
      // , ...
    });
 
    function x ()
    {
      for (var i = 0; i < len; ++i)
      {
        // opt[i] = ...
      }
    }

    // x(…)
  })();

– instead.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

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


#33679

FromAndrew Poulos <ap_prog@hotmail.com>
Date2017-08-02 14:06 +1000
Message-ID<G_2dnUe_rNVM0hzEnZ2dnUU7-YfNnZ2d@westnet.com.au>
In reply to#33678
On 1/08/2017 8:40 PM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
> 
>> There's some code that looks like
>>
>> var con = (function() {
>>     var opt0 = {},
>>         opt1 = {},
>>         opt2 = {},
>>         opt3 = {},
>>         ...
> 
> Who is doing such nonsense?

It happens gradually. First, there's just one opt(ion). A few weeks 
later the designer changes the design and so pt1 gets added. And so on...

A theorist told me years ago to always build things with the 
understanding that the number of items a design requires can change. 
Some of the people I work follow instructions explicitly and if it says 
"there's one item" then they build accordingly.

>>     function x() {
>>       //
>>     }
>> })()
>>
>> Within the function x I need to reference each of the (many) "opt"
>> objects.
> 
> Objects have identity, not name. > >> I'd like to reference them in a for loop e.g.
>>
>> for (i = 0; i < len; i++) {
>>     eval("opt" + i) = ...
>> }
>>
>> but how can I do it without using eval?
> 
> You cannot.  Therefore, use an Array instance –
> 
>    var con = (function () {
>      var opt = [
>        {},
>        {},
>        {},
>        {}
>        // , ...
>      ];
>   
>      function x ()
>      {
>        for (var i = 0, len = opt.length; i < len; ++i)
>        {
>          // opt[i] = ...
>        }
>      }
> 
>      // x(…)
>    })();
> 
> – or an Object instance –
> 
>    var con = (function () {
>      /* “opt” then refers to an object that does not inherit properties */
>      var opt = Object.assign(Object.create(null), {
>        0: {},
>        1: {},
>        2: {},
>        3: {}
>        // , ...
>      });
>   
>      function x ()
>      {
>        for (var i = 0; i < len; ++i)
>        {
>          // opt[i] = ...
>        }
>      }
> 
>      // x(…)
>    })();
> 
> – instead.

Ok, I did as you and Evertjan suggested and the change was easy and any 
increase in the number of opts will now be painless.

Andrew Poulos

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


#33681

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-08-02 11:38 +0200
Message-ID<3175537.R56niFO833@PointedEars.de>
In reply to#33679
Andrew Poulos wrote:

> On 1/08/2017 8:40 PM, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>>> There's some code that looks like
>>>
>>> var con = (function() {
>>>     var opt0 = {},
>>>         opt1 = {},
>>>         opt2 = {},
>>>         opt3 = {},
>>>         ...
>> 
>> Who is doing such nonsense?
> 
> It happens gradually. First, there's just one opt(ion). A few weeks
> later the designer changes the design and so pt1 gets added. And so on...
> 
> A theorist told me years ago to always build things with the
> understanding that the number of items a design requires can change.
> Some of the people I work follow instructions explicitly and if it says
> "there's one item" then they build accordingly.

As a rule of thumb, I start using arrays when there is more then one item of 
a kind.

> Ok, I did as you and Evertjan suggested and the change was easy and any
> increase in the number of opts will now be painless.

See also my supplementary posting.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

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


#33680

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-08-02 11:33 +0200
Message-ID<1922855.Mh6RI2rZIc@PointedEars.de>
In reply to#33678
Thomas 'PointedEars' Lahn wrote:

> Andrew Poulos wrote:
>> I'd like to reference them in a for loop e.g.
>> 
>> for (i = 0; i < len; i++) {
>>    eval("opt" + i) = ...
>> }
>> 
>> but how can I do it without using eval?
> 
> You cannot.  Therefore, use an Array instance –
> 
>   […]
> 
> – or an Object instance –
> 
>   var con = (function () {
>     /* “opt” then refers to an object that does not inherit properties */
>     var opt = Object.assign(Object.create(null), {
>       0: {},
>       1: {},
>       2: {},
>       3: {}
>       // , ...
>     });
>  
>     function x ()
>     {
>       for (var i = 0; i < len; ++i)
>       {
>         // opt[i] = ...
>       }
>     }
> 
>     // x(…)
>   })();
> 
> – instead.

Note that, with exceptions, the better approach is not to rely on the scope 
chain, but to pass a reference to the pertaining object:
  
  var con = (function () {
    /* “opt” then refers to an object that does not inherit properties */
    var opt = Object.assign(Object.create(null), {
      0: {},
      1: {},
      2: {},
      3: {}
      // , ...
    });
 
    function x (obj)
    {
      for (var i = 0; i < len; ++i)
      {
        // obj[i] = ...
      }
    }

    // x(opt)
  })();

This way, not only is the effective scope chain shorter, which is faster, 
but also the function code can be moved elsewhere for reuse without 
necessarily modifying it further.  For example,

  function x (obj)
  {
    for (var i = 0; i < len; ++i)
    {
      // obj[i] = ...
    }
  }

  var con = (function () {
    /* “opt” then refers to an object that does not inherit properties */
    var opt = Object.assign(Object.create(null), {
      0: {},
      1: {},
      2: {},
      3: {}
      // , ...
    });
 
    // x(opt)
  })();

works just as well.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


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


csiph-web