Class 1

A class abstracts the common part of an object. It refers generally to a class.

Object refers to a particular object and instantiates a concrete object through a class

1.1 create the class

class Star { constructor(name, age) { this.name = name; this.age = age; {}} sing () dance () {}} / / create var xx = new Star classes () / / instance objectsCopy the code
  • The constructor() method is the class’s constructor(default method), used to pass arguments, return an instance object, and is automatically called when an instance of the object is generated using the new command. If no definition is shown, a constructor() is automatically created for us inside the class.
  • Methods cannot be separated by commas, and methods do not need the function keyword.

1.2 Class inheritance

The extends keyword

  class Father {
      constructor(name) {
          this.name = name;
      }
      say() {}
  }
  class Son extends Father {}
  var son = new Son('xx');
  son.name = xx;
  son.say();
Copy the code

Through extends, Son inherits the properties and methods of Father.

The super keyword

class Father { constructor(x,y) { this.x=x; this.y=y } sum() { console.log(this.x+this.y); }} class Son extends Father{constructor(x,y){super()}} var Son = new Son(3,4); son.sum(); / / 7Copy the code
  • The super keyword is used to access and invoke functions on the object’s parent class. You can call either the constructor of the parent class or the normal function of the parent class
  • This in constructor points to the instance object, and this in a method points to the caller of the method
  • If a subclass uses super in its constructor, it must precede this.

2. Constructors

2.1 Introduction to constructors

Prior to ES6, objects were not created based on classes, but were defined with special functions called constructor functions and their characteristics. A constructor is a special function used to initialize an object, that is, to assign initial values to its member variables. It is always used with new. We can extract some common properties and methods from objects and encapsulate them in this function.

 function Father() {}
 var dada = new Father();
Copy the code

2.2 inheritance

Call ()

fn.call(thisArg, arg1, arg2, ...) 
Copy the code

ThisArg: the object currently called this refers to; Arg1, arg2: Other arguments to pass

Call () points this of the parent type to this of the child type, so that the child type inherits the attributes of the parent type.

function Person(name, age, sex) { this.name = name; this.age = age; this.sex = sex; } function Student(name, age, sex, score) { Person.call(this, name, age, sex); // The parent's this refers to the subclass's this, and the function is called. this.score = score; } var xx = new Student(' c ', 18, 'c ', 100); console.log(xx.age); / / 18Copy the code

The prototype object inherits the parent method

In general, the object’s methods are set in the prototype object of the constructor, and the parent method cannot be inherited through the constructor. There are three steps to inherit a parent class method:

  1. Prototype = new parent ();
  2. Essence: Subclassing the prototype object is like instantiating the parent class, because the parent class instantiates another space and does not affect the original parent class
  3. Redirect the constructor of a subclass to the subclass’s constructor

Differences between classes and constructors

  • A class is essentially a function.
  • All methods of a class are defined on the class’s Prototype property
  • Class, which also has __proto__ pointing to the class’s prototype object
  • So most of the functions of ES6 classes can be done in ES5. The new class writing method just makes the writing of object prototypes clearer and more like the syntax of object-oriented programming.

Practice class 4

Wrote a Tab bar, with a switch, the function of add, delete, modify, interested can look at the duheng678. Making. IO/class – demo -…