JSFiddle - React, Tailwind, and code Playground
by Charissima
JavaScript
function Car () {
var that = this;
this.totalDistance = 0;
this.putTotalDistance = function(distance) {
that.totalDistance = distance;
};
this.getTotalDistance = function() {
return this.totalDistance;
};
this.drive = function(distance) {
that.totalDistance += distance;
return that.totalDistance;
};
this.privateFunc = function() {
return 'car ' + this.totalDistance;
};
};
function RaceCar (initialDistance) {
var that = this;
this.prototype = new Car();
this.drive = function(distance) {
return that.prototype.drive(2*distance);
};
this.privateFunc = function() {
return 'raceCar ' + that.getTotalDistance();
};
};
RaceCar.prototype = new Car();
car = new Car;
raceCar = new RaceCar;
document.write('Test 1:<br>');
document.write(car.drive(10) + '<br>');
document.write(car.drive(5) + '<br>');
car.putTotalDistance(200);
document.write(car.getTotalDistance() + '<br>');
document.write(car.drive(10) + '<br><br>');
document.write('Test 2:<br>');
document.write(raceCar.drive(10) + '<br>');
document.write(raceCar.drive(5) + '<br>');
raceCar.putTotalDistance(200);
document.write(raceCar.getTotalDistance() + '<br>');
document.write(raceCar.drive(10) + ' Why not 220?<br><br>');