
Originally Posted by
eruna
Thanks for these tips. I don't understand some of this syntax. Could you explain a few things?
In the section below, what does the '.from' do?
Code:
from = subdivided_output[i].from;
To clarify, the "from =" is a completely different "from" than the one used in ".from" at the end.
One is a named variable, while the other is an object property.
Previously, push was being used to add items to the subdivided_output array:
Code:
subdivided_output.push(Array(output[i],output[ii]);
Using the push method with only one item tends to be slower than other techniques, so to squeeze more performance out of this section, we can use the array length to add the item to the end of that array.
It's also preferable to use [arrayItem] instead of new Array(arrayItem) too.
I also renamed the for loops to make it clear what they are referring to.
Code:
subdivided_output[subdivided_output.length] = [output[from],output[to]];
Later on, we had lots of different array index being used:
Code:
sub_coord1=subdivided_output[iii][0][0] + ...
So make things nice and clear, we can store the array items as named objects instead.
Code javascript:
subdivided_output[subdivided_output.length] = {'from': output[from], 'to': output[to]};
Those output items are also objects, which we'll get in to below, so code to access the subdivided_output values can now be:
Code:
sub_coord1=subdivided_output[iii]['from']['x'] + ...
Which can also be done without the array notation, as:
Code:
sub_coord1=subdivided_output[iii].from.x + ...
Using ['from'] or .from, those are not array variables. They are just ways to access object properties.
That line of code might be easier to understand if the variables are adjusted slightly, so that instead of using from/to to store the coord, we use start/end instead.
That way we would have code like this:
Code javascript:
start = subdivided_output[i].from;

Originally Posted by
eruna
In this section, how is "===" diferent from "=="
The triple equals is used to ensure that both side really are equal.
The double equal allows far too many strange cases to slip through, and programmers are almost certainly not aware of all variations, so it's best advised to stay away from using the double equals.
Some of this is explained in articles such as The pleasures and perils of JavaScript’s promiscuous comparison operator or in slides like The Good Parts Part Two (slides 31 & 32) which you can also see explained in good detail in JavaScript: The Good Parts [from 14:59] (video)

Originally Posted by
eruna
and how are x and y defined?
x and y are defined in the shape function. They used to be array items, such as:
Code:
points=new Array(x,y);
output.push(points);
which is the same as doing:
Code:
output[i] = [x, y];
however, storing them as array items results in needing to use [0] and [1] to access their values, and they are actually different types of values, so it makes better sense and is easier to read when we store and access them as x and y properties instead.
Code:
output[i] = {
x: Math.round(size * (1 + Math.cos(angle))),
y: Math.round(size * (1 + Math.sin(angle)))
};
It also means that what used to be this:
Code:
function shape(sides, size){
output=new Array;
x=0;
y=0;
for (i = 0; i < (sides+1); i++) {
x= size * Math.cos(2 * Math.PI * i / sides);
y=size * Math.sin(2 * Math.PI * i / sides);
x= x+size;
y= y+size;
x=Math.round(x);
y=Math.round(y);
points=new Array(x,y);
output.push(points);
}
return output;
}
is now easier to understand as:
Code javascript:
function shape(sides, size) {
Math.TAU = Math.PI * 2;
var output = [],
i,
slices = Math.TAU / sides,
angle;
for (i = 0; i <= sides; i += 1) {
angle = slices * i;
output[i] = {
x: Math.round(size * (1 + Math.cos(angle))),
y: Math.round(size * (1 + Math.sin(angle)))
};
}
return output;
}
Bookmarks