07.30《JavaScript》——模拟继承

Posted justlive-tears

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了07.30《JavaScript》——模拟继承相关的知识,希望对你有一定的参考价值。

模拟继承的三种方式:

1.call()

function Person(name, age) {
                this.name = name;
                this.age = age;
                this.eat = function(name) {
                    alert(this.name +  "正在吃饭");
                }
            }

            function Student(sno, name, age) {
                Person.call(this, name, age);
                this.sno = sno;
                this.study = function() {
                    alert("学号为"+this.sno+"的学生,姓名叫做"+this.name+",正在努力学习,年龄为"+this.age);
                }
            }
            
        
            var stu = new Student(1,‘wang‘,23);
            
            stu.eat();
            stu.study();

 

2.apply()

3.原型链继承prototype

以上是关于07.30《JavaScript》——模拟继承的主要内容,如果未能解决你的问题,请参考以下文章