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


Groups > comp.lang.javascript > #25970

Re: My 'actual' code

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: My 'actual' code
Date 2014-10-21 21:23 +0200
Organization PointedEars Software (PES)
Message-ID <2072711.hqF0VMn4kS@PointedEars.de> (permalink)
References <calc4vFrm5iU1@mid.individual.net> <can8usFboljU1@mid.individual.net> <caniujFef23U1@mid.individual.net> <87r3y16ysi.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

>   var all = [
>       { cells: getobj("ihsrow").cells,
>         string: ""
>       },
>       { cells: getobj("ipsrow").cells,
>         string: ""
>       },
>       { cells: getobj("ifsrow").cells,
>         string: ""
>       },
>       { cells: getobj("idsrow").cells,
>         string: ""
>       },
>       { cells: getobj("irsrow").cells,
>         string: ""
>       }
>   ];

This is asking for a DRY LART.
 
> though I'd be tempted to build these using a function -- only one letter
> is needed to create the different objects.

A function is not necessary:

  var names = ["ihsrow", "ipsrow", "ifsrow", "idsrow", "irsrow"];
  var all = [];

  for (var i = 0, len = names.length; i < len; ++i)
  {
    all.push({cells: getobj(names[i]).cells, string: ""});
  }

But (with an implementation of ECMAScript Ed. 5’s Array.prototype.map) with 
regard to code length at least it can be optimized to

  var all = names.map(function (name) {
    return {cells: getobj(name).cells, string: ""};
  });

and maybe even (longer, though)

  var all = "hpfds".split("").map(function (letter) {
    return {cells: getobj("i" + letter + "srow").cells, string: ""};
  });
 
I suggest avoiding the identifier “all”, though: it is not self-explanatory, 
and already used by a deprecated property of the object referred to by 
“document”, which could interfere in an HTML context.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


Thread

JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-20 15:11 -0600
  Re: JS Array problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:25 +0000
    Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:33 -0600
  Re: JS Array problem danca <cyberdanny@tiscalinet.it> - 2014-10-21 09:08 +0200
    Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:42 +0200
      Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 09:47 +0200
        Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:51 +0200
          Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 09:53 +0200
        Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:52 +0200
          Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 10:36 +0200
      Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:36 -0600
    Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:35 -0600
      Re: JS Array problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 19:02 +0200
  Re: JS Array problem "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-21 13:12 +0200
    Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:38 -0600
  Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:29 -0600
    My 'actual' code "Mel Smith" <syntel@cox.net> - 2014-10-21 11:20 -0600
      Re: My 'actual' code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-10-21 19:52 +0100
        Re: My 'actual' code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 21:23 +0200
  Re: JS Array problem Violet Deng <dengviolet@gmail.com> - 2014-10-28 02:03 -0700
    Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-28 10:08 +0100

csiph-web