javascript 继承


prtyaa
prtyaa 2023-12-26 18:45:56 63670
分类专栏: 资讯
 <script>
              
            window.onload=function(){
                function Person(name,age){
                    this.name=name;
                    this.age=age;
                }
                Person.prototype.getName=function(){
                    return this.name;
                }
                Person.prototype.getAge=function(){
                    return this.age;
                }
                
                function Teacher(name,age,subject){
                    Person.call(this,name,age);
                    this.subject=subject;
                }
                Teacher.prototype=Object.create(Person.prototype);
                Teacher.prototype.constructor=Teacher;
                Teacher.prototype.getSubject=function(){
                    return this.subject;
                }
                Teacher.prototype.getInfo=function(){
                    return "new"+this.getName();
                }
                var t=new Teacher("louis",30,"javascript");
                console.log(t.getInfo());
                
              var p=new Person("tina",25);
              console.log(p.getName());
                
              console.log(t instanceof Teacher);
              console.log(t instanceof Person);
            };

        </script>

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=31043
赞同 0
评论 0 条