@@ -146,4 +146,56 @@ describe('mergeDeep', function() {
146
146
assert . notEqual ( actual . keys , 42 ) ;
147
147
assert . notEqual ( actual . constructor . keys , 42 ) ;
148
148
} ) ;
149
+
150
+ it ( 'should allow being used for custom constructors' , function ( ) {
151
+ // The following setup code is a simple way to demonstrate multiple inheritance by merging the prototype of one class onto another
152
+ function Shape ( ) {
153
+ this . type = '' ;
154
+ }
155
+
156
+ function Position ( x , y ) {
157
+ this . x = x || 0 ;
158
+ this . y = y || 0 ;
159
+ }
160
+
161
+ Position . prototype . stringify = function ( ) {
162
+ return '(' + this . x + ', ' + this . y + ')' ;
163
+ } ;
164
+
165
+ function Moveable ( x , y ) {
166
+ Position . call ( this , x , y ) ;
167
+ }
168
+
169
+ // By making Moveable inherit from Position, allows us to test what happens when `constructor` is passed to `isValidKey`.
170
+ Moveable . prototype = Object . create ( Position . prototype ) ;
171
+ Moveable . prototype . constructor = Moveable ;
172
+ Moveable . prototype = merge ( Moveable . prototype , Position . prototype ) ;
173
+
174
+ Moveable . prototype . move = function ( x , y ) {
175
+ this . x += x ;
176
+ this . y += y ;
177
+ } ;
178
+
179
+ Moveable . prototype . position = function ( ) {
180
+ return this . stringify ( ) ;
181
+ } ;
182
+
183
+ function Rectangle ( ) {
184
+ Shape . call ( this ) ;
185
+ Moveable . call ( this ) ;
186
+ this . type = 'rectangle' ;
187
+ }
188
+
189
+ // Single inheritance using Object.create
190
+ Rectangle . prototype = Object . create ( Shape . prototype ) ;
191
+ Rectangle . prototype . constructor = Rectangle ;
192
+
193
+ // This is the test to ensure that `merge-deep` can be used with prototypal inheritance
194
+ Rectangle . prototype = merge ( Rectangle . prototype , Moveable . prototype ) ;
195
+
196
+ var rectangle = new Rectangle ( ) ;
197
+ assert . equal ( rectangle . position ( ) , '(0, 0)' ) ;
198
+ rectangle . move ( 10 , 20 ) ;
199
+ assert . equal ( rectangle . position ( ) , '(10, 20)' ) ;
200
+ } ) ;
149
201
} ) ;
0 commit comments