272 CHAPTER 6 Essential JavaScript and jQuery
this.make = theMake;
this.model = theModel;
}
Vehicle.prototype.getInfo = function () {
return 'Vehicle: ' + this.year + ' ' + this.make + ' ' + this.model;
}
By using this class and the prototype, you can write the following test to ensure that each
instance has its own data and that the getInfo function works properly.
test("Instance Test Using Prototype", function () {
expect(2);
var car1 = new Vehicle(2000, 'Ford', 'Fusion');
var car2 = new Vehicle(2010, 'BMW', 'Z4');
var expected = 'Vehicle: 2000 Ford Fusion';
var actual = car1.getInfo();
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
var expected = 'Vehicle: 2010 BMW Z4';
var actual = car2.getInfo();
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
});
In this test, two instances of the Vehicle class are created, each having different data. The
first assertion calls getInfo on car1 and verifies that the proper result is returned. The second
assertion calls getInfo on car2 and verifies that the proper result is returned. The result is
shown in Figure 6-5.
FIGURE 6-5 he modified class using the prototype property to create the getInfo functionT