1 function BaseClass() {
2 //父类对象showMsg方法
3 this.showMsg=function(){
4 alert("BaseClass::showMsg");
5 }
6 //父类对象的baseShowMsg方法
7 this.baseShowMsg=function(){
8 alert("BaseClass::baseShowMsg");
9 }
10 }
11 //父类的showMsg方法
12 BaseClass.showMsg=function(){
13 alert("BaseClass::showMsg static");
14 }
15
16 function ExtendClass() {
17 //子类对象的showMsg方法
18 this.showMsg=function(){
19 alert("ExtendClass::showMsg");
20 }
21 }
22 //子类的showMsg方法
23 ExtendClass.showMsg=function(){
24 alert("ExtendClass::showMsg static");
25 }
26
27 //测试
28 ExtendClass.prototype=new BaseClass();//子类的原型指向父类的一个实例
29 var instance1=new ExtendClass();//子类的一个实例instance1
30 instance1.showMsg();//如果子类对象有showMsg方法就调用之,没有就去原型链上查找有无此方法。
31 instance1.baseShowMsg();//当前对象没有此方法,所以去原型链上找,找到调用
32 ExtendClass.showMsg.call(instance1);//子类的对象去调用子类的方法
33 ExtendClass.showMsg();//调用类的方法,应该用类去调用而不能用该类的实例去调用
34 BaseClass.showMsg.call(instance1);//子类对象调用父类的showMsg方法
35 var baseInstance1=new BaseClass();
36 baseInstance1.showMsg.call(instance1);//子类对象调用父类对象的方法
1 function Animal () {
2 this.name="an animal";
3 this.age=3;
4 this.say=function() {
5 alert(this.name+' say:I am an animal,'+this.age+' years old.');
6 }
7 }
8 function Dog () {
9 this.name='a dog';
10 this.say=function () {
11 alert(this.name+' say:I am a dog,'+this.age+' years old.');
12 }
13 this.keepGete=function() {
14 alert(this.name+' is keeping Gate!');
15 }
16 }
17 function Cat () {
18 this.name='a cat'
19 this.catchMouse=function() {
20 alert(this.name+' is catching mouse!')
21 }
22 }
23 //Animal的原型上增加run方法,所有继承Animal类的子类都可以调用
24 Animal.prototype.run = function() {
25 alert(this.name+' is running!')
26 };
27
28 animal=new Animal();
29 animal.say();
30 animal.run();
31
32 Cat.prototype=new Animal();
33 cat=new Cat();
34 cat.say();
35 cat.run();
36 cat.catchMouse();
37
38 Dog.prototype=new Animal();
39 dog=new Dog();
40 dog.say();
41 dog.run();
42 dog.keepGete();