Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-将类重构为JS对象_Javascript_Oop_Ecmascript 6_Refactoring - Fatal编程技术网

Javascript-将类重构为JS对象

Javascript-将类重构为JS对象,javascript,oop,ecmascript-6,refactoring,Javascript,Oop,Ecmascript 6,Refactoring,我正在将当前的Firebase类拆分为更小的片段 export default class Firebase { constructor() { if (!firebase.apps.length) { // Initialize App firebase.initializeApp(Constants.manifest.web.config.firebase); // Initialize APIs this._usersAPI =

我正在将当前的Firebase类拆分为更小的片段

export default class Firebase {
  constructor() {
    if (!firebase.apps.length) {
      // Initialize App
      firebase.initializeApp(Constants.manifest.web.config.firebase);

      // Initialize APIs
      this._usersAPI = new Users();
      this._contentAPI = new Content();
      this._messagingAPI = new Messaging();
      this._notificationsAPI = new Notifications();
    }
  }

  get usersAPI() {
    return this._usersAPI;
  }

  ...
}
如您所见,Firebase类由较小的类组成

但是,老实说,smalle类似乎不需要作为类实现

现在,我正在考虑将它们移动到JS对象

export default class Auth {
  constructor() {
    this.auth = firebase.auth();
    this.firestore = firebase.firestore();
  }

  /*
    Persistance
  */

  enableAuthPersistence() {
    return this.auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL);
  }

  /*
    Sign in/out
  */

  signInWithEmailAndPassword(email, password) {
    return this.auth.signInWithEmailAndPassword(email, password);
  }

  async signInWithUsernameAndPassword(username, password) {
    ...
  }

  signOut() {
    return this.auth.signOut();
  }

  /*
    Password
  */

  resetPassword(email) {
    return this.auth.sendPasswordResetEmail(email);
  }

  updatePassword(password) {
    return this.auth.currentUser.updatePassword(password);
  }

  /*
    Helpers
  */

  parseError(errorCode) {
    ...
  }

  get currentUser() {
    return this.auth.currentUser;
  }
}
如何将它们转换为对象?所以我喜欢

  import users from "./api/users";
 
  ...


  constructor() {
     ...
     // Initialize APIs
     this._usersAPI = users;
     this._contentAPI = content;
     this._messagingAPI = messaging;
     this._notificationsAPI = notifications;
  }

  ...

在我的Firebase类中,不实例化?

只需将通常在构造函数中初始化的内容作为对象文本的属性,所有方法和getter/setter与同一对象文本上的速记方法和getter/setter类似:

export default {
  auth: firebase.auth(),
  firestore: firebase.firestore(),

  /*
    Persistance
  */
  enableAuthPersistence() {
    return this.auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL);
  },

  /*
    Sign in/out
  */
  signInWithEmailAndPassword(email, password) {
    return this.auth.signInWithEmailAndPassword(email, password);
  },

  async signInWithUsernameAndPassword(username, password) {
    ...
  },

  signOut() {
    return this.auth.signOut();
  },

  /*
    Password
  */
  resetPassword(email) {
    return this.auth.sendPasswordResetEmail(email);
  },
  updatePassword(password) {
    return this.auth.currentUser.updatePassword(password);
  },

  /*
    Helpers
  */
  parseError(errorCode) {
    ...
  },
  get currentUser() {
    return this.auth.currentUser;
  },
};

所有的改变都是
构造函数
,以及元素之间添加了一些逗号。

实际上JS中的所有内容都是对象中的(不是从对象继承的)。类只是调用Object.create()并进行一些原型设计的简单方法。也许你应该看看js中的静态类方法。代码中的最小更改。只需使用全局变量和函数,如
let userApi=new Users();导出函数getUser(){/*…*/}
等等?