1
1
/* eslint class-methods-use-this: 0 */
2
+ /* eslint no-param-reassign: 0 */
3
+
2
4
const X = Symbol . for ( 'x' ) ;
3
5
const Y = Symbol . for ( 'y' ) ;
4
6
const Z = Symbol . for ( 'z' ) ;
5
7
const DEFAULT = Symbol . for ( 'default' ) ;
6
8
7
9
let inProgress = DEFAULT ;
10
+ let inVector ;
8
11
9
12
const v3ValueOf = new Map ( ) ;
10
- v3ValueOf [ X ] = function getX ( ) {
11
- return this [ inProgress ] ;
12
- } ;
13
- v3ValueOf [ Y ] = function getY ( ) {
14
- return this [ inProgress ] ;
15
- } ;
16
- v3ValueOf [ Z ] = function getZ ( ) {
13
+ v3ValueOf [ X ] = function getValueOf ( ) {
14
+ if ( ! inVector ) {
15
+ inVector = this ;
16
+ }
17
17
return this [ inProgress ] ;
18
18
} ;
19
+ v3ValueOf [ Y ] = v3ValueOf [ X ] ;
20
+ v3ValueOf [ Z ] = v3ValueOf [ X ] ;
19
21
v3ValueOf [ DEFAULT ] = function getDefault ( ) {
20
22
return this . length ;
21
23
} ;
@@ -28,18 +30,27 @@ function innerCalc(alg, result) {
28
30
throw new Error ( 'something wrong' ) ;
29
31
}
30
32
try {
31
- const res = result ;
32
-
33
33
inProgress = X ;
34
- res [ inProgress ] = alg ( ) ;
34
+ const x = alg ( ) ;
35
+
36
+ if ( ! result && ! inVector ) {
37
+ return x ;
38
+ }
35
39
inProgress = Y ;
36
- res [ inProgress ] = alg ( ) ;
40
+ const y = alg ( ) ;
37
41
inProgress = Z ;
38
- res [ inProgress ] = alg ( ) ;
42
+ const z = alg ( ) ;
39
43
40
- return res ;
44
+ if ( ! result ) {
45
+ return inVector . createVector ( x , y , z ) ;
46
+ }
47
+ result [ X ] = x ;
48
+ result [ Y ] = y ;
49
+ result [ Z ] = z ;
50
+ return result ;
41
51
} finally {
42
52
inProgress = DEFAULT ;
53
+ inVector = undefined ;
43
54
}
44
55
}
45
56
@@ -200,7 +211,7 @@ export class Victor extends AVector {
200
211
}
201
212
202
213
export function calc ( alg ) {
203
- return innerCalc ( alg , new Vector ( ) ) ;
214
+ return innerCalc ( alg ) ;
204
215
}
205
216
206
217
export default Vector ;
0 commit comments