Js 继承

 1 Function.prototype.extend = function(superClass){  
 2         if(typeof superClass === 'function'){//类式继承  
 3             var F = function(){}; //创建一个中间函数对象以获取父类的原型对象  
 4             F.prototype = superClass.prototype; //设置原型对象  
 5             this.prototype = new F(); //实例化F, 继承父类的原型中的属性和方法,而无需调用父类的构造函数实例化无关的父类成员  
 6             this.prototype.constructor = this; //设置构造函数指向自己  
 7             this.superClass = superClass; //同时,添加一个指向父类构造函数的引用,方便调用父类方法或者调用父类构造函数  
 8         } else if(typeof superClass === 'object'){ //方法的扩充  
 9             var pro = this.prototype;  
10             for(var k in superClass){  
11                 if(!pro[k]){ //如果原型对象不存在这个属性,则复制  
12                     pro[k] = superClass[k];  
13                 }  
14             }  
15         } else {  
16             throw new Error('fatal error:"Function.prototype.extend" expects a function or object');  
17         }  
18           
19         return this;  
20 };    
21 
22     function Person(name){
23         this.name=name;    
24     }
25     Person.prototype.getName=function(){
26         return this.name;
27     }
28     
29     function Author(name,books){
30         Author.superClass.call(this,name);
31         this.books = books;
32     }
33     //继承父类Person
34     Author.extend(Person);
35     //添加方法getBooks
36     Author.prototype.getBooks = function(){
37         return this.books;    
38     }
39     //重载getNames
40     Author.prototype.getName=function(){
41         var name = Auhtor.superClass.prototype.getName.call(this);
42         return name+" , Author of books"+this.getBooks();
43     }
44 
45     /*调用方式*/  
46     Author.extend({  
47         sayHello:function(){  
48             alert('Hello');  
49         }  
50     }).extend(Author.prototype);//原型也是对象,所以一样可以作为参数传入,这样便扩展了其他类的原型方法,有点类似多重继承。     
51 window.onload = function(){
52     var a = new Author();
53     a.sayHello();    
54 }

这是个常用的继承方式,从别的地方找的资料 我验证通过的,故我放这了!

上面的这样的写法,我是不习惯,感觉怪怪的! 也许是像多重继承的原因吧,而下面的是 单个继承了。看起来简单多了吧~!~

另外:在 createNew () 方法中,只要不是定义在 cat 对象上的方法和属性,都是私有的。

下面的写法会好点吧:

 1 var cat = {
 2         createNew:function(){
 3             var cat = {};
 4             cat.name ="大毛";
 5             cat.makeSound = function(){
 6                 alert("喵喵喵");
 7                 
 8             }
 9             return cat;    
10         }
11     
12     };
13 var Animal = {
14         createNew: function () {
15             var animal = {};
16             animal.sleep = function () { alert ("睡懒觉"); };
17             return animal;
18         }
19     };    
20 var catt={
21         createNew:function(){
22             var cat = Animal.createNew();
23             cat.name ="大毛";
24             //cat.sleep = function(){ alert("休息,休息一会!"); };//这个将被方法被覆盖
25             cat.makeSound = function(){
26                 alert("喵喵喵");
27                 
28             }
29             return cat;    
30         }
31     }    
32     
33 
34 window.onload = function(){
35     var cat1 = cat.createNew();
36     cat1.makeSound();
37     
38     var cat2 = catt.createNew();
39     cat2.sleep();
40 }
posted @ 2012-10-25 10:12  凌之城  阅读(135)  评论(0编辑  收藏  举报