@@ -54,6 +54,27 @@ var Westeros;
54
54
} ) ( ) ;
55
55
Army . Lord = Lord ;
56
56
57
+ var InstanceOfExample = ( function ( ) {
58
+ function InstanceOfExample ( ) {
59
+ }
60
+ InstanceOfExample . prototype . Execute = function ( ) {
61
+ var collection = [ ] ;
62
+ collection . push ( Object . create ( Knight ) ) ;
63
+ collection . push ( Object . create ( FootSoldier ) ) ;
64
+ collection . push ( new Lord ( ) ) ;
65
+ collection . push ( new Archer ( ) ) ;
66
+
67
+ for ( var i = 0 ; i < collection . length ; i ++ ) {
68
+ if ( collection [ i ] instanceof Westeros . Army . Knight )
69
+ collection [ i ] . printName ( ) ;
70
+ else
71
+ console . log ( "No match" ) ;
72
+ }
73
+ } ;
74
+ return InstanceOfExample ;
75
+ } ) ( ) ;
76
+ Army . InstanceOfExample = InstanceOfExample ;
77
+
57
78
var IfExample = ( function ( ) {
58
79
function IfExample ( ) {
59
80
}
@@ -97,7 +118,7 @@ var Westeros;
97
118
function SelectiveNamePrinterVisitor ( ) {
98
119
}
99
120
SelectiveNamePrinterVisitor . prototype . visit = function ( memberOfArmy ) {
100
- if ( memberOfArmy . _type == " Westeros.Army.Knight" ) {
121
+ if ( memberOfArmy instanceof Westeros . Army . Knight ) {
101
122
this . VisitKnight ( memberOfArmy ) ;
102
123
} else {
103
124
console . log ( "Not a knight" ) ;
@@ -113,5 +134,12 @@ var Westeros;
113
134
var Army = Westeros . Army ;
114
135
} ) ( Westeros || ( Westeros = { } ) ) ;
115
136
116
- var b = new Westeros . Army . VisitorExample ( ) ;
137
+ console . log ( "Instance of" ) ;
138
+ var a = new Westeros . Army . InstanceOfExample ( ) ;
139
+ a . Execute ( ) ;
140
+ console . log ( "Type of" ) ;
141
+ var b = new Westeros . Army . IfExample ( ) ;
117
142
b . Execute ( ) ;
143
+ console . log ( "Internal type" ) ;
144
+ var c = new Westeros . Army . VisitorExample ( ) ;
145
+ c . Execute ( ) ;
0 commit comments