Code
/*function Vehicle()
{}
Vehicle.prototype.count = 4;
Vehicle.prototype.inpounds = 4000;
Vehicle.prototype.refuel = function(){
return "Vehicle87";
}
Vehicle.prototype.maintasks = function(){
return "Vehiclemaintasks";
}
function SportCat(){}
SportCat.prototype = new Vehicle();
SportCat.prototype.inpounds= 3000;
SportCat.prototype.refuel = function(){
return "SportCat95"
}
SportCat.prototype.maintasks = function(){
return "SportCatmaintasks";
}
function Truck(){}
Truck.prototype = new Vehicle();
Truck.prototype.count = 10;
Truck.prototype.inpounds = 12000;
Truck.prototype.refuel = function(){
return "Truck10";
}
Truck.prototype.maintasks = function(){
return "Truckmaintasks";
}*/
function Vehicle()//通过getpounds 访问私有属性
{
var count = 4;
var pounds = 4000;
this.getcount = function()
{return count;}
this.setcount = function(Count)
{count = Count}
this.getpounds = function()
{return pounds;}
this.setpounds = function(Pounds)
{pounds = Pounds}
this.refuel = function()
{return "Vehicle87";}
this.maintasks = function()
{return "Vehiclemaintasks";}
}
function SportsCar()
{
this.refuel = function() {
return "Refueling SportsCar with premium 94 octane gasoline";
}
this.mainTasks = function() {
return "Spirited driving, looking good, driving to the beach";
}
}
function CementTruck()
{
this.refuel = function() {
return "Refueling CementTruck with diesel fuel";
}
this.mainTasks = function() {
return "Arrive at construction site, extend boom, deliver cement";
}
}
/*function Vehicle()
{}
Vehicle.prototype.count = 4;
Vehicle.prototype.inpounds = 4000;
Vehicle.prototype.refuel = function(){
return "Vehicle87";
}
Vehicle.prototype.maintasks = function(){
return "Vehiclemaintasks";
}
function SportCat(){}
SportCat.prototype = new Vehicle();
SportCat.prototype.inpounds= 3000;
SportCat.prototype.refuel = function(){
return "SportCat95"
}
SportCat.prototype.maintasks = function(){
return "SportCatmaintasks";
}
function Truck(){}
Truck.prototype = new Vehicle();
Truck.prototype.count = 10;
Truck.prototype.inpounds = 12000;
Truck.prototype.refuel = function(){
return "Truck10";
}
Truck.prototype.maintasks = function(){
return "Truckmaintasks";
}*/
function Vehicle()//通过getpounds 访问私有属性
{
var count = 4;
var pounds = 4000;
this.getcount = function()
{return count;}
this.setcount = function(Count)
{count = Count}
this.getpounds = function()
{return pounds;}
this.setpounds = function(Pounds)
{pounds = Pounds}
this.refuel = function()
{return "Vehicle87";}
this.maintasks = function()
{return "Vehiclemaintasks";}
}
function SportsCar()
{
this.refuel = function() {
return "Refueling SportsCar with premium 94 octane gasoline";
}
this.mainTasks = function() {
return "Spirited driving, looking good, driving to the beach";
}
}
function CementTruck()
{
this.refuel = function() {
return "Refueling CementTruck with diesel fuel";
}
this.mainTasks = function() {
return "Arrive at construction site, extend boom, deliver cement";
}
}
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Classical Inheritance in JavaScript</title>
<script type="text/javascript" src="classicalInheritance.js"></script>
<script type="text/javaScript">
//createInheritance(new 父类,子对象)把父类中有的copy到子类 继承
function createInheritance(parent, child) {
var property;
for(property in parent) {
if(!child[property]) {
child[property] = parent[property];
}
}
}
function describe(vehicle) {
var description = "";
description = description + "Number of wheels (via property): " + vehicle.wheelCount;
description = description + "\n\nNumber of wheels (via accessor): " + vehicle.getWheelCount();
description = description + "\n\nCurb Weight (via property): " + vehicle.curbWeightInPounds;
description = description + "\n\nCurb Weight (via accessor): " + vehicle.getCurbWeightInPounds();
description = description + "\n\nRefueling Method: " + vehicle.refuel();
description = description + "\n\nMain Tasks: " + vehicle.mainTasks();
alert(description);
}
function createVehicle() {
var vehicle = new Vehicle();
describe(vehicle);
}
function createSportsCar() {
var sportsCar = new SportsCar();
createInheritance(new Vehicle(), sportsCar);
sportsCar.setCurbWeightInPounds(3000);
describe(sportsCar);
}
function createC()
{
var c = new Truck();
createInheritance(new Vehicle(),c);
}
function createCementTruck() {
var cementTruck = new CementTruck();
createInheritance(new Vehicle(), cementTruck);
cementTruck.setWheelCount(10);
cementTruck.setCurbWeightInPounds(10000);
describe(cementTruck);
}
</script>
</head>
<body>
<h1>Examples of Classical Inheritance in JavaScript</h1>
<br/><br/>
<button onclick="createVehicle();">Create an instance of Vehicle</button>
<br/><br/>
<button onclick="createSportsCar();">Create an instance of SportsCar</button>
<br/><br/>
<button onclick="createCementTruck();">Create an instance of CementTruck</button>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Classical Inheritance in JavaScript</title>
<script type="text/javascript" src="classicalInheritance.js"></script>
<script type="text/javaScript">
//createInheritance(new 父类,子对象)把父类中有的copy到子类 继承
function createInheritance(parent, child) {
var property;
for(property in parent) {
if(!child[property]) {
child[property] = parent[property];
}
}
}
function describe(vehicle) {
var description = "";
description = description + "Number of wheels (via property): " + vehicle.wheelCount;
description = description + "\n\nNumber of wheels (via accessor): " + vehicle.getWheelCount();
description = description + "\n\nCurb Weight (via property): " + vehicle.curbWeightInPounds;
description = description + "\n\nCurb Weight (via accessor): " + vehicle.getCurbWeightInPounds();
description = description + "\n\nRefueling Method: " + vehicle.refuel();
description = description + "\n\nMain Tasks: " + vehicle.mainTasks();
alert(description);
}
function createVehicle() {
var vehicle = new Vehicle();
describe(vehicle);
}
function createSportsCar() {
var sportsCar = new SportsCar();
createInheritance(new Vehicle(), sportsCar);
sportsCar.setCurbWeightInPounds(3000);
describe(sportsCar);
}
function createC()
{
var c = new Truck();
createInheritance(new Vehicle(),c);
}
function createCementTruck() {
var cementTruck = new CementTruck();
createInheritance(new Vehicle(), cementTruck);
cementTruck.setWheelCount(10);
cementTruck.setCurbWeightInPounds(10000);
describe(cementTruck);
}
</script>
</head>
<body>
<h1>Examples of Classical Inheritance in JavaScript</h1>
<br/><br/>
<button onclick="createVehicle();">Create an instance of Vehicle</button>
<br/><br/>
<button onclick="createSportsCar();">Create an instance of SportsCar</button>
<br/><br/>
<button onclick="createCementTruck();">Create an instance of CementTruck</button>
</body>
</html>