JS继承

1.继承父类属性和方法,同时拥有自己的属性和方法。
2.每一个对象创建出来的时候,都初始化一个proto属性。
3.对象冒充:.call(this指向,参数列表)
.apply(this指向,[参数列表]);

继承方法:

(1).原型链
window.onload=function(){
        function Person(name,age){
                this.name=name;
                this.age=age;
                if(typeof this.show != "function"){
                        Person.prototype.show=function(){
                                alert("I am "+this.name);
                        }
                }
        }
        function Student(id,score){
                this.id=id;
                this.score=score;
                if(typeof this.show != "function"){
                        Student.prototype.show=function(){
                                alert("score is "+this.score);
                        }
                }
        }
        var s1=new Student();
        console.log(s1);
        s1.show(); //student里的show方法
        Student.prototype=new Person();
        var s2=new Student();
        console.log(s2);
        s2.show(); //是Person里的show方法
}
2.对象冒充
window.onload=function(){
        function Person(name,age){
                this.name=name;
                this.age=age;
                if(typeof this.show != "function"){
                        Person.prototype.show=function(){
                                alert("I am "+this.name);
                        }
                }
                console.log(name,age)
        }
        function Student(id,name,age,score){
                this.id=id;
                this.score=score;
                if(typeof this.show != "function"){
                         Student.prototype.show=function(){
                                 alert("score is "+this.score);
                         }
                }
                alert(this);
                Person.call(this,name,age);//对象冒充,call:将this指向Student,函数调用时原本指向Person;
        }
        var s=new Student("01","mm",18,180);
        console.log(s);
        s.show(); //调用student函数;
        //对象冒充 并非真正继承,而是通过 (调用函数) 改变this指向 冒充 被继承对象 创建 对象和属性,所以 似乎被继承对象 的原型里 的属性和方法 不能被 冒充对象 调用。
}
3.组合继承:对象冒充 + 原型继承
window.onload=function(){
        //组合继承 : 对象冒充 + 原型继承
        function Person(name,age){
                this.name=name;
                this.age=age;
                if(typeof this.show != "function"){
                        Person.prototype.show=function(){
                                alert("I am "+this.name);
                        }
                }
        }
        function Student(id,name,age,score){
                this.id=id;
                this.score=score;
                if(typeof this.show != "function"){
                        Student.prototype.show=function(){
                                alert("score is "+this.score);
                        }
                }
                Person.call(this,name,age);//对象冒充,call:将this指向Student,函数调用时原本指向Person;
        }
        var s1=new Student("01","mm",18,180);
        console.log(s1);
        s1.show();  // 调用student函数;person里的show函数不被继承!!!

        Student.prototype=new Person(); // 原型继承
        var s2=new Student("02","yy",19,200);
        //Student.prototype=new Person(); // 原型继承
        console.log(s2);
        s2.show();//调用Person里的函数show(因为Student里无函数show,注意顺序问题!!),若无判断,则调用Student里的函数show。
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值