js原型链继承

63 阅读1分钟

将父类的对象实例赋值子类的原型。

原型链继承的查找路线:对象实例--->对象的构造函数----->对象的原型----->父类实例------>父类构造函数---->父类的原型

     function Person(name, age) {
            this.name = name;
            this.age = age;
        }
 
        //方法
        Person.prototype.say = function () {
            console.log(this.name + " 在哈哈大笑");
        }
 
 
        //学生对象
        function Student(no, name, age) {
            this.no=no;
        }
 
        //将父类的对象实例赋值给子类的原型对象
        Student.prototype = new Person();//原型链继承
 
 
        Student.prototype.study = function () {
            console.log(this.name + " 在奋笔疾书....");
        }
 
        // var s = new Student();
        // console.log(s);
        // s.say()
        var s = new Student(1000,'小刚',6);
        console.log(s);
        s.say()