通过prototype属性建立面向对象的JavaScript
时间:2007-05-23 来源:rcymonkey
function Car(){}
Car.prototype.wheelCount = 4;
Car.prototype.color = "not set";
Car.prototype.go = function() {
return "Car.go";
}
Car.prototype.stop = function() {
return "stop";
}
function SportsCar(){}
SportsCar.prototype = new Car();
SportsCar.prototype.color = "green";
SportsCar.prototype.name = "Sports Car";
SportsCar.prototype.go=function(){
return "SportsCar go";
}
SportsCar.prototype.contest= function(){
return "contest";
}
function test() {
var sportsCar = new SportsCar();
alert(sportsCar.wheelCount);//4
alert(sportsCar.color);//green
alert(sportsCar.name);//Sports Car
alert(sportsCar.go());//SportsCar go
alert(sportsCar.stop());//stop
alert(sportsCar.contest());//contest
}