forked from fraxken/test-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.js
69 lines (63 loc) · 1.37 KB
/
02.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @typedef {Object} VectorLike
* @property {Number} x
* @property {Number} y
* @property {Number} z
*/
/**
* @class Vector
*
* @property {Number} x
* @property {Number} y
* @property {Number} z
*/
class Vector {
/**
* @constructor
* @memberof Vector#
* @param {Number} [x=0] x
* @param {Number} [y=0] y
* @param {Number} [z=0] z
*
* @throws {TypeError}
*/
constructor(x = 0, y = 0, z = 0) {
if (typeof x !== "number") {
throw new TypeError("x must be a number");
}
if (typeof y !== "number") {
throw new TypeError("y must be a number");
}
if (typeof z !== "number") {
throw new TypeError("z must be a number");
}
this.x = x;
this.y = y;
this.z = z;
}
/**
* @method add
* @memberof Vector#
* @param {!Vector} vec vector Object
* @returns {void}
*
* @throws {TypeError}
*/
add(vec) {
if (!(vec instanceof Vector)) {
throw new TypeError("vec must be an instanceof Vector");
}
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;
}
/**
* @method toJSON
* @memberof Vector#
* @returns {VectorLike}
*/
toJSON() {
return { x: this.x, y: this.y, z: this.z };
}
}
module.exports = Vector;