• Source code address

Use the class declaration to create a new class with a given name based on archetypal inheritance.

class Animal {
    // As in Java, there is no default empty constructor when specified
    constructor(name, age) {
        this.name = name;
        this.age = age; }}const a1 = new Animal('Dog egg'.3);
console.log(a1);
console.log(a1.name);
console.log(a1.age);
Copy the code

You can also define classes using class expressions. But unlike class expressions, class declarations do not allow you to re-declare an existing class, otherwise a type error will be thrown.

grammar

class name [extends] {
  // class body
}
Copy the code

Declare a class

In the example below, we first define a class named Polygon and then inherit it to create a class named Square. Note that the super() used in constructors can only be used in constructors and must be called before the this keyword is used.

class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
    this.name = 'Square'; }}Copy the code

Repeat class

Repeating a class declaration causes a type error.

class Foo {};
class Foo {};
// Uncaught TypeError: Identifier 'Foo' has already been declared
Copy the code

If a class was previously defined using a class expression, redeclaring that class also causes a type error.

let Foo = class {};
class Foo {};
// Uncaught TypeError: Identifier 'Foo' has already been declared
Copy the code

attribute

The properties that are accessed through new instances are called instance properties.

console.log(p1.name)
console.log(p1.age)
Copy the code

Static properties: Properties accessed directly through constructors are called static properties.

class Animal {
    constructor(name, age) {
        this.name = name;
        this.age = age; }}// Static attributes
Animal.info = "sss";
Copy the code

methods

Instance methods

Definition:

Animal.prototype.say = function () {
    console.log('Here's the animal instance method')}Copy the code

Class inheritance

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age; }}class Chinese extends Person {}class America extends Person {}const c1 = new Chinese('Java'.18);
const a1 = new America('Edge'.18);
console.log(c1)
console.log(a1)
Copy the code

Subclass constructor

The same is true in Java: if a subclass extends from its parent class via the extends keyword, then super() must be called first in the constructor function of the subclass.

Super is a function, and it’s the constructor of the superclass, so super in a subclass is actually a reference to the constructor constructor of the superclass.

If I just write a random call

class Chinese extends Person {
    constructor() {
        super();
    }
}

const c1 = new Chinese('Java'.18);
console.log(c1)
Copy the code

It’s not definedSo you need to write:

class Chinese extends Person {
    constructor(name, age) {
        super(name, age); }}Copy the code

So how do you use subclass-specific fields?

class Chinese extends Person {
    constructor(name, age, IDNo) {
        super(name, age);
        this.IDNo = IDNo; }}const c1 = new Chinese('Java'.18.'123 * * * * *');
Copy the code