Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34817
| Path | csiph.com!weretis.net!feeder4.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.222.224.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Trying to create array of object literals |
| Date | Fri, 13 Apr 2018 10:27:44 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 162 |
| Message-ID | <3364120.8dbtfUO1ph@PointedEars.de> (permalink) |
| References | <2ea2803e-c39d-40e0-b8ed-136c3dc9b4e0@googlegroups.com> <literal-20180412204347@ram.dialup.fu-berlin.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| Injection-Info | gwaiyur.mb-net.net; posting-host="222.224.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch:178.197.224.222"; logging-data="8302"; mail-complaints-to="abuse@open-news-network.org" |
| User-Agent | KNode/4.14.2 |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| Cancel-Lock | sha1:QNmm+sjsCPqBWHqo5SS8P+Awd88= |
| X-User-ID | U2FsdGVkX1/epDYAhj5uDZ3w4YZvh+9nKdQyNXo8RcxgP0K3OUuMzA== |
| Xref | csiph.com comp.lang.javascript:34817 |
Show key headers only | View raw
Stefan Ram wrote:
> bit-naughty@hotmail.com writes:
>>var point = {
>>x:0,
>>y:0
>>};
>>Now I want to build an array of the "point" array literal (ie. storing 3
>>points):
>
> A literal is solely an entity of the source code model,
> literals are not run-time values.
Also, it is _not_ an Array literal, but an _Object_ literal/initialiser,
equivalent to:
var point = new Object();
point.x = 0;
point.y = 0;
IOW, in both cases “point” holds a reference to an Object instance; “x” and
“y” are the names of the *properties* of that object, and the zeroes are the
*property values*.
JavaScript/ECMAScript *basics* (and told to the anonymous, address-munging¹
OP several times before²).
________
¹ <http://www.interhack.net/pubs/munging-harmful/>
<https://tools.ietf.org/html/rfc5536#section-3.1.2>
² <http://www.catb.org/esr/faqs/smart-questions.html>
>>var projectedpoly = point[3];
>
> You might want:
>
> { const p = []; for( let i = 0; i < 3; ++i )p.push({ x: 0, y: 0 }); }
Readable:
{
const polygon = [];
for (let i = 0; i < 3; ++i)
{
polygon.push({x: 0, y: 0});
}
}
Probably closer to what was *really* being looked for:
var point1 = {x: 1, y: 2};
var point2 = {x: 3, y: 4};
var point3 = {x: 5, y: 6};
var polygon = [point1, point2, point3];
or (functionally equivalent)
var polygon = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6}
];
or (functionally equivalent)
function Point (x, y)
{
this.x = x;
this.y = y;
}
var point1 = new Point(1, 2);
var point2 = new Point(3, 4);
var point3 = new Point(5, 6);
var polygon = [point1, point2, point3];
or (functionally equivalent)
function Point (x, y)
{
this.x = x;
this.y = y;
}
var polygon = [
new Point(1, 2),
new Point(3, 4),
new Point(5, 6)
];
then
/* an Object instance equivalent to {x: 5, y: 6} */
var point = polygon[2];
Definining a point as an instance of a custom object type (“Point” is its
constructor) has the advantages that all point objects automatically have
the same structure, can have the same methods (this allows polymorphism),
and details of the implementation are hidden from the user of the object
(information hiding). [The properties are still publicly accessible
directly here, but that can be fixed.]
In that sense, “polygon” should be a reference to an instance of a custom
object type as well (there are operations that apply to all polygons).
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide>
In order to DRY,
var polygon = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6}
];
can be converted thus:
function Point (x, y)
{
/* When called as a function, work as factory (type conversion) */
if (!(this instanceof Point)) return new Point(x, y);
if (x instanceof Object)
{
/*
* Assume a reference to an object to be converted;
* this also allows Point({x: 42, y: 23}) instead of
* Point(42, 23), getting rid of the opaqueness of
* arguments, and the dependency on argument order.
*/
this.x = x.x;
this.y = x.y;
}
else
{
this.x = x;
this.y = y;
}
}
polygon = polygon.map(Point);
This approach is very useful when dealing with JSON, e.g.:
var data = `[
{"x": 1, "y": 2},
{"x": 3, "y": 4},
{"x": 5, "y": 6}
]`;
var points = JSON.parse(data).map(Point);
“points” now refers to an Array instance of references to Point instances
that are initialized with the values in the JSON (and could itself be passed
as an argument to, e.g., a Polygon constructor/factory).
--
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Trying to create array of object literals bit-naughty@hotmail.com - 2018-04-12 12:17 -0700
Re: Trying to create array of object literals bit-naughty@hotmail.com - 2018-04-12 12:26 -0700
Re: Trying to create array of object literals Maik Koenig <usenetspam@maikkoenig.de> - 2018-04-12 21:47 +0200
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-12 12:49 -0700
Re: Trying to create array of object literals Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-13 10:27 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-13 10:57 +0100
Re: Trying to create array of object literals Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-13 23:38 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-14 11:22 +0100
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-14 07:25 -0700
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-14 09:09 -0700
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-16 09:43 +0100
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-16 02:04 -0700
Re: Trying to create array of object literals "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-16 11:23 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-16 19:32 +0100
Re: Trying to create array of object literals "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-17 23:37 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-18 16:18 +0100
Re: Trying to create array of object literals "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-19 23:08 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-20 10:17 +0100
Re: Trying to create array of object literals "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-20 12:45 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-20 19:08 +0100
Re: Trying to create array of object literals "c.l.js" <trollhunter@curiosity.kills.no> - 2018-04-20 17:04 -0300
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-20 13:15 -0700
Is JS a functional programming language? (was: Trying to create array of object literals) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-21 02:35 +0200
Re: Is JS a functional programming language? (was: Trying to create array of object literals) Julio Di Egidio <julio@diegidio.name> - 2018-04-21 01:36 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-21 11:46 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-21 11:48 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) Julio Di Egidio <julio@diegidio.name> - 2018-04-21 23:53 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-22 10:33 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) Julio Di Egidio <julio@diegidio.name> - 2018-04-23 00:27 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-23 11:50 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) Julio Di Egidio <julio@diegidio.name> - 2018-04-23 12:00 -0700
Re: Is JS a functional programming language? (was: Trying to create array of object literals) "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-23 15:07 -0700
Re: Is JS a functional programming language? "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-04-24 00:47 +0200
Re: Is JS a functional programming language? Julio Di Egidio <julio@diegidio.name> - 2018-04-23 16:12 -0700
Re: Is JS a functional programming language? "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-04-24 01:30 +0200
Re: Is JS a functional programming language? Julio Di Egidio <julio@diegidio.name> - 2018-04-23 16:45 -0700
Re: Is JS a functional programming language? John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-24 09:54 +0100
Re: Is JS a functional programming language? Tim Streater <timstreater@greenbee.net> - 2018-04-24 10:17 +0000
Re: Is JS a functional programming language? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-24 12:02 -0700
Re: Is JS a functional programming language? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-25 05:04 +0200
Re: Is JS a functional programming language? Julio Di Egidio <julio@diegidio.name> - 2018-04-25 02:16 -0700
Re: Is JS a functional programming language? Julio Di Egidio <julio@diegidio.name> - 2018-04-25 02:20 -0700
Re: Is JS a functional programming language? John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-25 18:22 +0100
Re: Is JS a functional programming language? Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-04-25 17:25 +0000
Re: Is JS a functional programming language? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-25 14:50 -0700
Re: Is JS a functional programming language? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-25 14:39 -0700
Re: Is JS a functional programming language? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-29 20:44 +0200
Re: Is JS a functional programming language? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-04-29 13:45 -0700
Re: Is JS a functional programming language? "c.l.js" <trollhunter@curiosity.kills.no> - 2018-04-21 19:51 -0300
Re: Is JS a functional programming language? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-22 01:18 +0200
Re: Is JS a functional programming language? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-22 02:19 +0200
Re: Is JS a functional programming language? "Mark-" <nospam@mapson.xyz> - 2018-04-22 01:52 +0000
Re: Is JS a functional programming language? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-22 04:30 +0200
Re: Is JS a functional programming language? "Mark-" <nospam@mapson.xyz> - 2018-04-22 02:45 +0000
Re: Is JS a functional programming language? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-22 05:55 +0200
Re: Is JS a functional programming language? "Mark-" <nospam@mapson.xyz> - 2018-04-22 04:04 +0000
Re: Is JS a functional programming language? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-22 10:59 +0200
Re: Is JS a functional programming language? John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-22 11:03 +0100
Re: Is JS a functional programming language? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-22 12:50 +0200
Re: Is JS a functional programming language? John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-23 09:19 +0100
Re: Is JS a functional programming language? "Mark-" <nospam@mapson.xyz> - 2018-04-22 12:58 +0000
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-16 10:02 +0100
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-16 02:05 -0700
Re: Trying to create array of object literals "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-16 11:27 +0200
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-16 02:33 -0700
Re: Trying to create array of object literals "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-04-16 14:03 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-16 19:40 +0100
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-17 09:45 +0100
Re: Trying to create array of object literals Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-18 03:25 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-18 15:50 +0100
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-18 16:08 +0100
Re: Trying to create array of object literals Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-19 03:34 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-19 10:37 +0100
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-19 19:02 +0100
Re: Trying to create array of object literals Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-19 03:25 +0200
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-16 19:37 +0100
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-16 11:48 -0700
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-17 09:52 +0100
Re: Trying to create array of object literals Julio Di Egidio <julio@diegidio.name> - 2018-04-17 03:22 -0700
Re: Trying to create array of object literals John G Harris <niam@jghnorth.org.uk.invalid> - 2018-04-17 18:28 +0100
Re: Trying to create array of object literals bit-naughty@hotmail.com - 2018-04-13 04:08 -0700
csiph-web