What is the class

A program structure that centrally stores a uniform property structure and method definition for all child objects of a type

How to define class

jsNative definition of:
    function Student(name,age){
      this.name= name ;
      this.age = age;
    }
    Student.prototype.intr= function(){
        console.log(I call `The ${this.name}; I this yearThe ${this.age}At the age of `)}var person = new Student('Joe'.18)
    person.intr(); My name is Zhang SAN; I am 18 years old
Copy the code

Note: Defined on global objects

Using class definitions:
  1. withclass{}Wrap the original constructor + prototype object method
  2. The original constructor nameUpgrade to the entireclassAll constructors are renamed toconstructor
  3. Method in the prototype object, no need to addprototypeFunction (){function (){function (); . }
    class Student {
      constructor(name, age) {
        this.name = name;
        this.age = age;
      }
      intr() { // Automatically put into the prototype object
        console.log(
          I call `The ${this.name}, IThe ${this.age}At the age of `); }}var person = new Student('Joe'.18)
    person.intr(); My name is Zhang SAN; I am 18 years old
Copy the code

Note: Defined in script

Question: Where are method definitions stored in class{}?

Results: all saved on the prototype object; The essence is the same, the data structure that appears after you print person is the same.

Question: If multiple child objects share the same attribute value, where should it be placed? How to access

Static className=” class 2″

Common property access: student.className

class{
    constructor(name, age) {
        this.name = name;
        this.age = age;
      }
    static className="Class 2, Grade 1"  // static common attribute name = attribute value
    intr() { // Automatically put into the prototype object
        console.log(
          I call `The ${this.name}, IThe ${this.age}At the age of `,${Student.className}); }}}Copy the code

The use of extends and super in class:

    class people{
        constructor(name,sex,age){
          this.name= name;
          this.sex= sex;
          this.age= age;
        }
         peopleAction(){
          console.log(I call `The ${this.name}, I amThe ${this.sex}This year, IThe ${this.age}At the age of `)}}class men extends people{
         constructor(name,sex,age,noBirthBaby){
            super(name,sex,age)
            this.noBirthBaby = noBirthBaby;
         }
         action(){
          console.log(I call `The ${this.name}, I amThe ${this.sex}This year, IThe ${this.age}Years old, IThe ${this.noBirthBaby}Raw Eva `)}}var tom = new men('Tom'.'male'.'5'.'not')
      tom.peopleAction() // My name is Tom, I'm male, I'm 5 years old -- using the superclass method
      tom.action()  // My name is Tom, I'm male, I'm 5 years old, I can't have a baby
      
      var Mill = new men('Mill'.'woman'.'21'.'can')
      Mill.action() // My name is Mill. I'm female. I'm 21 years old
Copy the code