Skip to content

Commit 2e4783a

Browse files
committed
Made notes on circular references.
1 parent 139c93b commit 2e4783a

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

test/circular.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
import {expect} from 'chai';
2-
import {Fruit} from './circular/fruit';
2+
3+
import {Basket} from "./circular/basket";
34
import {Peach} from './circular/peach';
45
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+
*/
628

729
describe('Create fruit', () => {
830
it('Can identify types of fruit', () => {

test/circular/fruit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import {Basket} from "./basket";
22
export class Fruit {
33
basket: Basket;
44
addNewBasket () {
5-
//this.basket = new Basket();
5+
this.basket = new Basket();
66
}
77
}

0 commit comments

Comments
 (0)