ES6 - 鶏はじめシリーズ〜クラス

コントラスト

9028759-3268cf935afbad1e.jpeg
ES6

ネイティブシミュレーション

//定义
function User(){
  this.username = 'lake';
  this.age = 24;
}
//增加方法
User.prototype.login = function(){
  //exec login service
}
//创建对象
let userObj = new User();
//执行方法
userObj.login();

クラスの使用

//定义
class User{
  constructor(age){
    this.username = 'lake';
    this.age = age;
  }

  login(username='lake',password){
    //exec login service
  }
}
//创建对象
let userObj = new User(24);
//调用方法
userObj(undefined,'lake');

受け継ぎます

class Person{
  
}
class User extends Person{
  // constructor(age){
  //  ...
}

staticメソッド

class User extends Person{
  static hi(){
    return 'hello';
  }
  // constructor(age){
  //  ...
}
//调用
console.log(User.hi());
//输出
> hello

静的メソッド(ロゴ)

class User extends Person{
  static get hi(){
    return 'hello';
  }
  // constructor(age){
  //  ...
}
//调用(不用写括号)
console.log(User.hi);
//输出
> hello

おすすめ

転載: blog.csdn.net/weixin_34393428/article/details/90840075