You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently repetitive typing is needed when calling some functions that take, for example, x, y, and z as parameters. This can lead to lengthy lines of code when these values come from a Vector3 with a long name. Implementing an expand method should save coding time and lead to cleaner more readable code.
Solution
// Add an expand method to, for example, the THREE.Vector3 prototype
THREE.Vector3.prototype.expand = function () {
return [this.x, this.y, this.z];
};
// Function that we'd like to pass coordinated to.
function xyzFunction(x, y, z) {
console.log(x, y, z);
}
// Use the expand method with the spread operator
const myVector = new THREE.Vector3(1, 2, 3);
xyzFunction(...myVector.expand()); // Outputs: 1 2 3
Alternatives
Overloading all of the functions that take x, y, z as inputs with versions that take Vector3 (don't like this as much)
Additional context
Seeing this used in examples will also expose newbies to the use of the spread operator.
The text was updated successfully, but these errors were encountered:
I would not recommend doing this since every time expand() is called the method returns a new array object. If this code is used in an animation loop multiple times, you increase the memory churn and thus end up with more GC overhead. Besides, object creation tends to be an expensive operation in JavaScript so you want to avoid it whenever possible. At least in performance critical code sections. So overall it's indeed better to write functions to accept Vector3 objects or alternatively pass in numerical values:
If you insist in working with arrays, you can use the existing myVector.toArray(), btw. We have added this for the serialization/deserialization logic but you can use it of course for other use cases as well. Just keep in mind the previous comment if you head for best performance.
Cool - its basically already been implemented. With toArray() you can even pass in an empty scratch array to avoid making lots of new calls when the code needs to be performant.
Description
Currently repetitive typing is needed when calling some functions that take, for example, x, y, and z as parameters. This can lead to lengthy lines of code when these values come from a Vector3 with a long name. Implementing an expand method should save coding time and lead to cleaner more readable code.
Solution
Alternatives
Overloading all of the functions that take x, y, z as inputs with versions that take Vector3 (don't like this as much)
Additional context
Seeing this used in examples will also expose newbies to the use of the spread operator.
The text was updated successfully, but these errors were encountered: