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; }
That said, it is cleaner to only 'var' each variable once, to suppress this confusion.