zoukankan      html  css  js  c++  java
  • JS继承

    ES5继承

    function Person(name, age) {
      this.name = name;
      this.age = age;
    }
    Person.prototype.sayName = function () {
      alert(`My name is ${this.name}.`);
      return this.name;
    }
    Person.prototype.constructor = Person;
    
    function Student(name, age, grade) {
      Person.call(this, name, age);
      this.grade = grade;
    }
    Student.prototype = Object.create(Person.prototype);
    Student.prototype.constructor = Student;
    Student.prototype.sayGrade = function () {
      alert(`I am a grade ${this.grade} student.`);
      return this.grade;
    }
    
    let s1 = new Student("Mike", 12, 4);
    let s2 = new Student("Helen", 18, 12);
    

    ES6继承

    class Person {
      constructor (name, age) {
        this.name = name;
        this.age = age;
      }
      sayName () {
        alert(`My name is ${this.name}.`);
        return this.name;
      }
    }
    class Student extends Person {
      constructor (name, age, grade) {
        super(name, age);
        this.grade = grade;
      }
      sayGrade () {
        alert(`I am a grade ${this.grade} student.`);
        return this.grade;
      }
    }
    let s1 = new Student("Mike", 12, 4);
    let s2 = new Student("Helen", 18, 12);
    
  • 相关阅读:
    使用控制台来启动.net core 的程序
    论钱的意义
    js 将图片转换为 base64
    CPU 的由来
    C# Cef winform 脚本的执行 踩过的坑
    什么是JSONP?
    Cookie和Session
    request
    response和ServletContext和乱码问题
    Servilet初步
  • 原文地址:https://www.cnblogs.com/buildnewhomeland/p/13123907.html
Copyright © 2011-2022 走看看