温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JS继承

发布时间:2020-09-21 02:21:09 来源:网络 阅读:216 作者:心月草 栏目:web开发

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。
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI