File tree 1 file changed +51
-0
lines changed
1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ function CarDoor ( options ) {
2
+ this . color = options . color || 'red' ;
3
+ this . side = options . side || 'right' ;
4
+ this . hasPowerWindows = options . hasPowerWindows || true ;
5
+ }
6
+
7
+ function CarSeat ( options ) {
8
+ this . color = options . color || 'gray' ;
9
+ this . material = options . material || 'leather' ;
10
+ this . isReclinable = options . isReclinable || true ;
11
+ }
12
+
13
+ function CarPartFactory ( ) { }
14
+
15
+ CarPartFactory . prototype . createPart = function createCarPart ( options ) {
16
+ var parentClass = null ;
17
+
18
+ if ( options . partType === 'door' ) {
19
+ parentClass = CarDoor ;
20
+ } else if ( options . partType === 'seat' ) {
21
+ parentClass = CarSeat ;
22
+ }
23
+
24
+ if ( parentClass === null ) {
25
+ return false ;
26
+ }
27
+
28
+ return new parentClass ( options ) ;
29
+ }
30
+
31
+ // example usage
32
+ var myPartFactory = new CarPartFactory ( ) ;
33
+ var seat = myPartFactory . createPart ( {
34
+ partType : 'seat' ,
35
+ material : 'leather' ,
36
+ color : 'blue' ,
37
+ isReclinable : false
38
+ } ) ;
39
+
40
+ // create a door with deault option
41
+ var door = myPartFactory . createPart ( { partType :'door' } ) ;
42
+
43
+ console . log ( seat instanceof CarSeat ) ; // logs 'true
44
+
45
+ //logs a CarSeat object with material "leather", color "blue", isReclinable "false"
46
+ console . log ( seat ) ;
47
+
48
+ console . log ( door instanceof CarDoor ) ; //logs 'true'
49
+ //logs a CarDoor object with color "red", side "right", hasPowerWindows "true"
50
+ console . log ( door ) ;
51
+
You can’t perform that action at this time.
0 commit comments