js 继承示例

    /* Extend Function */  
      
    function extend(subClass,superClass){  
        var Func = function(){} ;  
        Func.prototype = superClass.prototype ;  
        subClass.prototype = new Func() ;  
        subClass.prototype.constructor = subClass ;  

        subClass.superClass=superClass.prototype;
        if(superClass.prototype.constructor==Object.prototype.constructor){
           superClass.prototype.constructor=superClass;
        }
    } ;  
      
    /*-----Example-----*/  
      
      
    /* Class Person */  
    function Person(name){  
        this.name = name ;  
    } ;  
      
    Person.prototype.getName = function(){  
        return this.name ;  
    } ;  
      
    /* Class Author */  
      
    function Author(name,books){  
        Person.call(this,name) ;  
        this.books = books ;  
    } ;  
      
    extend(Author,Person) ;  
      
    Author.prototype.getBooks = function(){  
        return this.books ;  
    } ;   

 

posted @ 2014-12-11 00:46  bert.zeng  阅读(94)  评论(0)    收藏  举报