Class

The User class is compiled and converted to a constructor. When compiled, the _classCallCheck,_instanceof methods are generated.

class User{}const user = new User();
console.log(user); / / {}

/ / write write write write write write write write write write write write write ES6 write write write write write write write write write write write write write write
/ / left left left left left left left left left left down down down down down down down down down left left left ES5 left left left left left left left left left left left left left left down down down down down down down down down

"use strict";

function _instanceof(left, right) {
  if( right ! =null &&
    typeof Symbol! = ="undefined" &&
    right[Symbol.hasInstance]
  ) {
    return right[Symbol.hasInstance](left);
  } else {
    return left instanceofright; }}function _classCallCheck(instance, Constructor) {
  if(! _instanceof(instance, Constructor)) {throw new TypeError("Cannot call a class as a function"); }}var User = function User() {
  _classCallCheck(this, User);
};

var user = new User();
console.log(user); / / {}

Copy the code

Common properties

The User class has three attributes: name, age and Address. The attributes of name and age are defined by object. defineProperty. Although the assignment is different from that of address, the end result is the same.

Note: Name and age are not new ways of writing attribute instance attributes defined on the stereotype

class User{
    name = "jason";
    age = 18;
    constructor() {this.address = "shanxi"; }}const user = new User();
console.log(user); //{ name: 'jason', age: 18, address: 'shanxi' }

/ / write write write write write write write write write write write write write ES6 write write write write write write write write write write write write write write
/ / left left left left left left left left left left down down down down down down down down down left left left ES5 left left left left left left left left left left left left left left down down down down down down down down down

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true.configurable: true.writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}

var User = function User() {
  _classCallCheck(this, User);

  _defineProperty(this."name"."jason");

  _defineProperty(this."age".18);

  this.address = "shanxi";
};
Copy the code

The following code omits common methods like _defineProperty,_classCallCheck,_instanceof, etc.

Common function

SayName calls _defineProperties(Constructor. Prototype, protoProps) on the Constructor’s prototype;

class User{
  name = "jason";
  sayName(){
    console.log(this.name); }}const user = new User();
user.sayName()

/ / write write write write write write write write write write write write write ES6 write write write write write write write write write write write write write write
/ / left left left left left left left left left left down down down down down down down down down left left left ES5 left left left left left left left left left left left left left left down down down down down down down down down

function _defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
    var descriptor = props[i];
    descriptor.enumerable = descriptor.enumerable || false;
    descriptor.configurable = true;
    if ("value" in descriptor) descriptor.writable = true;
    Object.defineProperty(target, descriptor.key, descriptor); }}function _createClass(Constructor, protoProps, staticProps) {
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  if (staticProps) _defineProperties(Constructor, staticProps);
  return Constructor;
}

var User =
  (function() {
    function User() {
      _classCallCheck(this, User);

      _defineProperty(this."name"."jason");
    }

    _createClass(User, [
      {
        key: "sayName".value: function sayName() {
          console.log(this.name); }}]);returnUser; }) ();var user = new User();
user.sayName()
Copy the code

Arrow function

The arrow function binds this after compilation

class User{
  name = "jason";
  sayName = (a)= > {
    console.log(this.name); }}/ / write write write write write write write write write write write write write ES6 write write write write write write write write write write write write write write
/ / left left left left left left left left left left down down down down down down down down down left left left ES5 left left left left left left left left left left left left left left down down down down down down down down down

var User = function User() {
  var _this = this;

  _classCallCheck(this, User);

  _defineProperty(this."name"."jason");

  _defineProperty(this."sayName".function() {
    console.log(_this.name);
  });
};
Copy the code

Static attributes

The static property exists as a property of the constructor

class User{
  static name = "jason";
}

/ / write write write write write write write write write write write write write ES6 write write write write write write write write write write write write write write
/ / left left left left left left left left left left down down down down down down down down down left left left ES5 left left left left left left left left left left left left left left down down down down down down down down down

var User = function User() {
  _classCallCheck(this, User);
};

// omit the _defineProperty method source code
// Static attributes are compiled with User as the first argument, while normal attributes are this
_defineProperty(User, "name"."jason");
Copy the code

A static method

The static method sayName exists as an attribute of the constructor

class User {
  name = "jason"
  static sayName() {

  }
}

/ / write write write write write write write write write write write write write ES6 write write write write write write write write write write write write write write
/ / left left left left left left left left left left down down down down down down down down down left left left ES5 left left left left left left left left left left left left left left down down down down down down down down down

function _defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
    var descriptor = props[i];
    descriptor.enumerable = descriptor.enumerable || false;
    descriptor.configurable = true;
    if ("value" in descriptor) descriptor.writable = true;
    Object.defineProperty(target, descriptor.key, descriptor); }}function _createClass(Constructor, protoProps, staticProps) {
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  if (staticProps) _defineProperties(Constructor, staticProps);
  return Constructor;
}
var User =
/*#__PURE__*/
function () {
  function User() {
    _classCallCheck(this, User);

    _defineProperty(this."name"."jason");
  }
  After the static function is compiled, the second argument is set to null
  _createClass(User, null[{key: "sayName".value: function sayName() {}}]);returnUser; } ();Copy the code