RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
JS继承

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

我们提供的服务有:做网站、网站制作、微信公众号开发、网站优化、网站认证、渌口ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的渌口网站制作公司

继承方法:

(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。
}

网站栏目:JS继承
本文地址:http://scyingshan.cn/article/pdihis.html