Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: My 'actual' code Date: Tue, 21 Oct 2014 21:23:31 +0200 Organization: PointedEars Software (PES) Lines: 57 Message-ID: <2072711.hqF0VMn4kS@PointedEars.de> References: <87r3y16ysi.fsf@bsb.me.uk> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1413919412 27286 eJwFwYEBwCAIA7CXRNrCzlGQ/09YQpepAqLA4WgQ2ZuPkHqfsGd1fbhiXTCPdQuocUx/O38TUBDa (21 Oct 2014 19:23:32 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 21 Oct 2014 19:23:32 +0000 (UTC) User-Agent: KNode/4.14.1 X-User-ID: eJwFwYEBACAEBMCVhH81jtD+I3QH42KFE3Q8vLFg8d4MwgTOgjWg6/Rp3u2bE1qTUsGxSWS0HFfTEZX3AUOwFQU= Cancel-Lock: sha1:qsIsBrIN5R317mG/GhW5P3fhxO0= X-NNTP-Posting-Host: eJwFwQkRwEAIBDBLZWF55FAY/Eu4hOriE+Z04/HyYhC4v2akt4vAigJyXdosBdcuNJn1wR8ehxB5 Xref: csiph.com comp.lang.javascript:25970 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.