首页 > 技术文章 > 07.30《JavaScript》——模拟继承

justlive-tears 2018-07-31 00:09 原文

模拟继承的三种方式:

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

推荐阅读