Stefan Ram wrote:
>
bit-n...@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.