Js中借用构造函数的 组合继承.

释放双眼,带上耳机,听听看~!
//借用构造函数的 组合继承. 

        //声明了一个构造函数Person
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }

        Person.prototype = {
            constructor: Person,
            sayHi: function () {
                console.log('你好,我的名字是' + this.name);
            }
        }
        //声明一个学生构造函数Student
        function Student(name, age, score) {
            //借用Person构造函数继承
            Person.call(this, name, age);
            this.score = score;
        }

        //替换原型继承. 
        Student.prototype = new Person();

        Student.prototype.sayHello = function () {
            console.log('哈哈哈.学生的sayHello');
        }

        //实例化学生对象
        let s1 = new Student('张三', 19, 100);
        console.log(s1);
        s1.sayHi();
        s1.sayHello();

        //实例化人对象
        let p1 = new Person('李四', 19);
        console.log(p1);
        p1.sayHi();
内容投诉
JavaScript

Js中求数组的最大值和最小值的两种方法

2020-7-27 18:09:06

JavaScript

Ajax中onreadystatechange事件/方法详解

2020-8-11 17:12:39

搜索