File tree 2 files changed +25
-3
lines changed
2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { expect } from 'chai' ;
2
- import { Fruit } from './circular/fruit' ;
2
+
3
+ import { Basket } from "./circular/basket" ;
3
4
import { Peach } from './circular/peach' ;
4
5
import { Pear } from './circular/pear' ;
5
- import { Basket } from "./circular/basket" ;
6
+ import { Fruit } from './circular/fruit' ;
7
+
8
+ /***
9
+ * Circular referencese
10
+ *
11
+ * You will get a run-time error "Object prototype may only be an Object or null: undefined at line 7 of peach.js" if your
12
+ * imports are such that Fruit is imported first as in ....
13
+ import {Fruit} from './circular/fruit';
14
+ import {Peach} from './circular/peach';
15
+ import {Pear} from './circular/pear';
16
+ import {Basket} from "./circular/basket";
17
+ *
18
+ * This is because the order or processing will be:
19
+ * 1) Fruit is invoked. Fruit imports basket.
20
+ * 2) Basket then imports Peach and Pear
21
+ * 3) Peach and Pear then import Fruit (which is already in process of being imported and so undefined is returned)
22
+ * 4) Since Peach extends Fruit and Fruit is undefined an incorrect class is generated
23
+ * 5) When an instance Fruit is created it fails
24
+ *
25
+ * However by importing Basket first, Basket and Fruit are ready by the time the extends comes along
26
+ *
27
+ */
6
28
7
29
describe ( 'Create fruit' , ( ) => {
8
30
it ( 'Can identify types of fruit' , ( ) => {
Original file line number Diff line number Diff line change @@ -2,6 +2,6 @@ import {Basket} from "./basket";
2
2
export class Fruit {
3
3
basket : Basket ;
4
4
addNewBasket ( ) {
5
- // this.basket = new Basket();
5
+ this . basket = new Basket ( ) ;
6
6
}
7
7
}
You can’t perform that action at this time.
0 commit comments