• JavaScript ES6中的继承


     <script>
    
            /*
            // ES6之前的继承
            function Person(myName, myAge) {
                this.name = myName;
                this.age = myAge;
                this.say = function () {
                    console.log(this.name, this.age);
                };
            }
    
            function Studnt(myName, myAge, myScore) {
                // 通过call函数借用父类的构造函数
                Person.call(this, myName, myAge);
                this.score = myScore;
                this.learn = function () {
                    console.log("day day up");
                };
            }
            // 将子类的原型对象更改为父类的实例对象
            Studnt.prototype = new Person();
            Studnt.prototype.constructor = Studnt;
            let stu = new Studnt("peter", 20, 50);
            stu.say();
            console.log(stu);
            */
    
    
            // ES6中的继承
            class Person{
                constructor(myName, myAge){
                    this.name = myName;
                    this.age = myAge;
                }
    
                say(){
                    console.log(this.name, this.age);
                }
            }
    
            class Student extends Person{
                constructor(myName, myAge, myScore){
                    super(myName, myAge);
                    this.score = myScore;
                }
                learn = function(){
                    console.log("day day up");
                }
            }
    
            let stu = new Student("tm", 12, 100);
            console.log(stu);
            stu.say();
        </script>
    
  • 相关阅读:
    面向对象之prototype,__proto__
    Screen对象
    location对象
    history对象
    JS计时器
    window对象
    Dom操作html详细
    终端 git log 修改样式
    null 和 NULL 判断
    textfield设置左边距
  • 原文地址:https://www.cnblogs.com/TomHe789/p/12722900.html
Copyright © 2020-2023  润新知