Skip to content
Advertisement

A Vector Class in Javascript

I’m trying to implement a vector object instantiated like below…

JavaScript

…and when I do a math operation I need something like this…

JavaScript

…but my code below returns me just an array

JavaScript

Please help me out here. Thank you!

Advertisement

Answer

You need to wrap up your resulting array in a new Vector object:

JavaScript

I should note also that you may want to do further reading on creating JavaScript objects, in the area of creating methods (such as your add method) on the prototype of the Vector object. There are many good tutorials out there.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement