Javascriptオブジェクトの継承
3051 ワード
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>javascriptInherited.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<script type="text/javascript">
//Javascript
//
function Car(sColor){
this.sColor = sColor;
this.showColor = function(){
alert(this.sColor);
};
}
//inherit Car
function MyCar(sColor,sMoney){
this.newMethods = Car;
this.newMethods(sColor);
//
// , ,
//
delete this.newMethods;
this.sMoney = sMoney;
this.showMoney = function(){
alert(sMoney);
};
}
// call(),apply()
function ClassA(sColor){
this.sColor = sColor;
}
ClassA.prototype.showColor = function(){
alert(this.sColor);
};
//inherit
function ClassB(sColor,fMoney){
// , arguments
ClassA.call(this,sColor);
//ClassA.apply(this,new Array(sColor));
//ClassA.apply(this,arguments);
this.fMoney = fMoney;
}
ClassB.prototype = new ClassA();
ClassB.prototype.showMoney = function(){
alert(this.sColor+" "+this.fMoney);
};
//
function ClassF(sColor){
this.sColor = sColor;
if(typeof ClassF._init == "undefined"){
ClassF.prototype.showColor = function(){
alert(this.sColor);
};
ClassF._init = true;
}
}
//Inherit ClassF
function ClassS(sColor,iMoney){
ClassF.apply(this,arguments);
this.iMoney = iMoney;
if(typeof ClassS._inits == "undefined"){
ClassS.prototype.showMoney = function(){
alert(this.sColor+" "+this.iMoney);
};
ClassS._inits = true;
}
}
// ,
ClassS.prototype = new ClassF();
window.onload = function(){
//
//var mycar = new MyCar("red",50);
//mycar.showMoney();
//mycar.showColor();
//var myclass = new ClassB("red",20);
//myclass.showColor();
//myclass.showMoney();
var autoClass = new ClassS("blue",3000);
autoClass.showMoney();
autoClass.showColor();
}
</script>
<body>
</body>
</html>