js原型链继承及调用父类方法

方法1:
 1  var Parent= function () {
 2 
 3     };
 4 
 5     Parent.prototype.process = function(){
 6         alert('parent method');
 7     };
 8 
 9     var Child= function () {
10         Parent.call(this);
11     };
12 
13     Child.prototype = new Parent();
    Child.prototype.constructor=Child;
14 Child.prototype.process = function(){ 15 Parent.prototype.process.apply(this, arguments);16 alert('child method'); 17 }; 18 19 var childVar = new Child(); 20 childVar.process();

 方法2

function(){
    //创建一个人员类
    function Person(name){
        this.name = name;
    }
    //创建教师类
    function Teacher(name,books){
        //call方法可以将一个函数的对象上下文从初始化变成有this来决定
        //调用Person的构造函数,因为Person没用new 所以他是个空对象
        //相当于java中的super函数
        Person.call(this,name);
        this.books = books;
    }
    //使老师类继承人员类
    Teacher.prototype = new Person();
    Teacher.prototype.constructor = Teacher;
    Teacher.prototype.getBook = function(){
        return this.name +" "+ this.books;
    }
    //测试
    var jim = new Teacher("JIM","EXTJS4");
    //alert(jim.getBook());
    
    /**
     * 创建Extend函数为了程序中石所有的集成操作
     */
    function extend(subClass,superClass){
        //1.叫子类原型类属性等于父类的原型属性
        //初始化一个中间空对象,为了转换主父类关系
        var F = function(){};
        F.prototype = superClass.prototype;
        //2.让子类集成F
        subClass.prototype = new F();
        subClass.prototype.constructor = subClass;
        //3.为子类增加属性superClass
        subClass.superClass = superClass.prototype;
        //4.增加一个保险,就算你是的原型类是超类(Object) 那么也要把你的构造函数级别讲下来
        if(superClass.prototype.constructor == Object.prototype.constructor){
            superClass.prototype.constructor = superClass;
        }
    }
    //测试
    function Author(name,books){
        Author.superClass.constructor.call(this,name);
        
        this.books = books;
        this.getBook = function(){
            
        console.log(Author.superClass.getBook.apply(this, arguments)) ;
        
            return this.name +" "+ this.books;
        }
    }
    //继承
    extend(Author,Teacher);
    
    var peter = new Author("YUNFENGCHENG","JAVASCIPT");
   
    console.log(peter.getBook());
    ;
})()

 

 
posted @ 2018-05-27 14:58  catgatp  阅读(5332)  评论(0编辑  收藏  举报