JavaScript中的面向对象----继承
1、对象冒充
function ClassA(sColor){
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function ClassB(sColor,sName){
this.newMethod = ClassA;
this.newMethod(sColor);
delete this.newMethod;
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function ClassB(sColor,sName){
this.newMethod = ClassA;
this.newMethod(sColor);
delete this.newMethod;
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
2、call方法
function ClassA(sColor){
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function ClassB(sColor,sName){
// this.newMethod = ClassA;
// this.newMethod(sColor);
// delete this.newMethod;
ClassA.call(this,sColor);
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function ClassB(sColor,sName){
// this.newMethod = ClassA;
// this.newMethod(sColor);
// delete this.newMethod;
ClassA.call(this,sColor);
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
3、apply方法
function ClassA(sColor){
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function ClassB(sColor,sName){
// this.newMethod = ClassA;
// this.newMethod(sColor);
// delete this.newMethod;
ClassA.apply(this,new Array(sColor));
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function ClassB(sColor,sName){
// this.newMethod = ClassA;
// this.newMethod(sColor);
// delete this.newMethod;
ClassA.apply(this,new Array(sColor));
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
4、原型链方法
function ClassA(){
}
ClassA.prototype.color = "color";
ClassA.prototype.sayColor = function(){
alert(this.color);
}
function ClassB(){
}
ClassB.prototype = new ClassA();
ClassB.prototype.name = "name";
ClassB.prototype.sayName = function(){
alert(this.name);
}
var objB = new ClassB();
objB.color = "color of objB";
objB.name = "name of objB";
objB.sayColor();
}
ClassA.prototype.color = "color";
ClassA.prototype.sayColor = function(){
alert(this.color);
}
function ClassB(){
}
ClassB.prototype = new ClassA();
ClassB.prototype.name = "name";
ClassB.prototype.sayName = function(){
alert(this.name);
}
var objB = new ClassB();
objB.color = "color of objB";
objB.name = "name of objB";
objB.sayColor();
5、混合方式
function ClassA(sColor){
this.color = sColor;
}
ClassA.prototype.sayColor = function(){
alert(this.color);
}
function ClassB(sColor,sName){
ClassA.call(this,sColor);
this.name = sName;
}
ClassB.prototype = new ClassA();
ClassB.prototype.sayName = function(){
alert(this.name);
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();
this.color = sColor;
}
ClassA.prototype.sayColor = function(){
alert(this.color);
}
function ClassB(sColor,sName){
ClassA.call(this,sColor);
this.name = sName;
}
ClassB.prototype = new ClassA();
ClassB.prototype.sayName = function(){
alert(this.name);
}
var objB = new ClassB("color of objB","name of objB");
objB.sayColor();