11/* eslint class-methods-use-this: 0 */
2+ /* eslint no-param-reassign: 0 */
3+
24const X = Symbol . for ( 'x' ) ;
35const Y = Symbol . for ( 'y' ) ;
46const Z = Symbol . for ( 'z' ) ;
57const DEFAULT = Symbol . for ( 'default' ) ;
68
79let inProgress = DEFAULT ;
10+ let inVector ;
811
912const 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+ }
1717 return this [ inProgress ] ;
1818} ;
19+ v3ValueOf [ Y ] = v3ValueOf [ X ] ;
20+ v3ValueOf [ Z ] = v3ValueOf [ X ] ;
1921v3ValueOf [ DEFAULT ] = function getDefault ( ) {
2022 return this . length ;
2123} ;
@@ -28,18 +30,27 @@ function innerCalc(alg, result) {
2830 throw new Error ( 'something wrong' ) ;
2931 }
3032 try {
31- const res = result ;
32-
3333 inProgress = X ;
34- res [ inProgress ] = alg ( ) ;
34+ const x = alg ( ) ;
35+
36+ if ( ! result && ! inVector ) {
37+ return x ;
38+ }
3539 inProgress = Y ;
36- res [ inProgress ] = alg ( ) ;
40+ const y = alg ( ) ;
3741 inProgress = Z ;
38- res [ inProgress ] = alg ( ) ;
42+ const z = alg ( ) ;
3943
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 ;
4151 } finally {
4252 inProgress = DEFAULT ;
53+ inVector = undefined ;
4354 }
4455}
4556
@@ -200,7 +211,7 @@ export class Victor extends AVector {
200211}
201212
202213export function calc ( alg ) {
203- return innerCalc ( alg , new Vector ( ) ) ;
214+ return innerCalc ( alg ) ;
204215}
205216
206217export default Vector ;
0 commit comments