3 ms·
I don't like using var in that way, because it seems like you're declaring two distinct variables. In your example, I'd much prefer: function make_dot_produ
by codeflo 12y ago
I don't like using var in that way, because it seems like you're declaring two distinct variables. In your example, I'd much prefer:
function make_dot_product(dim) {
var dot;
if (dim === 3) {
dot = function(a, b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
};
} else if (dim === 2) {
dot = function(a, b) {
return a.x * b.x + a.y * b.y;
};
}
return dot;
}
This also makes it clearer what happens if neither dim is neither 2 nor 3.
- jackweirdy 12y agoThe variables are the same. Var statements get hoisted to the beginning of the function. That said, it is cleaner to only 'var' each variable once, to suppress this confusion.