Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Lesson 1: Creating JavaScript objects CHAPTER 6 281


This completes Car, and Boat is similar except that Boat has a propellerBladeQuantity,
which is initialized to three, instead of the wheelQuantity property. In addition, getInfo
returns the vehicle type of Boat and calls the Vehicle getInfo method as follows.
var Boat = (function (parent) {
Boat.prototype = new Vehicle();
Boat.prototype.constructor = Boat;
function Boat(year, make, model) {
parent.call(this, year, make, model);
this.propellerBladeQuantity = 3;
}
Boat.prototype.getInfo = function () {
return 'Vehicle Type: Boat ' + parent.prototype.getInfo.call(this);
};
return Boat;
})(Vehicle);

In addition to the Vehicle tests already presented, you need to verify the following for the
child classes.
■■Car and Boat have the inherited year, make, and model properties.
■■Car has its wheelQuantity property and it's set.
■■Boat has its propellerBladeQuantity and it's set.
■■Car and Boat return the proper value from the replaced getInfo method.
■■Car and Boat return the proper value from the inherited startEngine method.
The following are the Car and Boat tests.
test('Car Inheritance Test', function () {
expect(6);
var c = new Car(2012, 'Toyota', 'Rav4');
var actual = c.year;
var expected = 2012;
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
var actual = c.make;
var expected = 'Toyota';
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
var actual = c.model;
var expected = 'Rav4';
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
var actual = c.wheelQuantity;
var expected = 4;
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
var actual = c.getInfo();
var expected = 'Vehicle Type: Car 2012 Toyota Rav4';
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
Free download pdf