日期:2014-05-16 浏览次数:20515 次
view plain
function classA(color){
this.color = color;
this.show = function(){alert(this.color);}
}
/*
Note:
1> this 代表的是classA函数所构建的对象,而非函数classA对象本身这样说主要是为了避免(function is object)的影响;
2> 在构建classA对象时,是通过this来初始化的属性和方法的,如果用classB的this去冒充classA的this,那么就实现了简单的继承了
*/
view plain
function classA(sColor){
this.sColor = sColor;
this.show = function(){alert(this.sColor);}
}
function classB(sColor,sName){
this.new = classA;
this.new(sColor);
delete this.new;
/*
Note
必须释放classA的引用,那样对于classA是安全的。可是再执行一段以上代码,实现多重继承。
this.new = classC;
this.new(arguments);
delete this.new;
*/
this.sName = sName;
this.say = function(){alert(this.sName);}
}
var a = new classA("blue");
var b = new classB("red","BMW");
a.show(); // blue
b.say(); //BMW
b.show(); // red view plain
function classA(){}
classA.prototype.sColor = "red";
classA.prototype.show = function(){
alert(this.sColor);
}
function classB(){}
classB.prototype = new classA(); //no arguments
/*
Note:
classB.prototype 开始指向一块命名空间,执行上行语句后,classB.prototype的命名空间则是classA的以一个对象,所有在执行此方法前不可以向classB增加属性和方法,不然会发生覆盖。
*/
classB.prototype.name = "BMW";
classB.prototype.say = function(){
alert(this.name);
}
var a = new classA();
a.show(); // red
var b = new classB();
b.show(); // red
b.say(); //BMW
view plain
function classA(sColor){
this.sColor = sColor;
this.show = function(){alert(this.sColor);}
}
classA.prototype.say = function(){
alert("hello");
}
function classB(sColor,sName){
classA.call(this,sColor);
this.sName = sName;
this.showName = function(){
alert(this.sName);
}
}
var a = new classA("blue");
a.show();//blue
a.say();//hello
var b = new classB("red","BMW");
b.show();//red
b.say();//error : no this function
b.showName();// BMW view plain
function classA(sColor){
this.sColor = sColor;
this.show = function(){alert(this.sColor);}
}
classA.prototype.say = function(){
alert("hello..");
}
function classB(sColor,sName){
classA.apply(this,new Array(sColor));
this.sName = sName;
this.showName = function(){
alert(this.sName);
}
}
var a = new classA("blue");
a.show();// output: blue
a.say();// output: hello..
var b =