简体   繁体   English

Javascript - 正确使用 class 的方法

[英]Javascript - Correct way to use class

I wrote a mqtt client.js, but realized I need multiple instances of this script so I rewrote it into a class.我写了一个 mqtt client.js,但意识到我需要这个脚本的多个实例,所以我将它重写为 class。 Now I ended up with a lot of 'this.foo' references vor my variables, which made me wonder, is this the correct way of using classes in javascript?现在我得到了很多 'this.foo' 引用 vor 我的变量,这让我想知道,这是在 javascript 中使用类的正确方法吗?

Here´s how part of my class looks like:这是我的 class 的一部分的样子:

      this.client = mqtt.connect("mqtts://" + this.host, options); // mqtts for tls connection

      // client event handling
      this.client.on("connect", () => {
        log.write("LOG_INFO", "PubClient connected to Broker: " + this.host + ":" + this.settings.port);
        this.client.publish("client/publisher/status", "online", { qos: 1, retain: true });
        this.clientToSocket.pubConnect(true);
      });
      this.client.on("close", () => {
        //log.write("LOG_INFO", "PubClient close");
      });
      this.client.on("disconnect", (packet) => {
        log.write("LOG_INFO", "PubClient disconnected. Reasoncode: " + packet.reasoncode);

      });
      this.client.on("offline", () => {
        log.write("LOG_INFO", "PubClient offline");
        this.clientToSocket.pubConnect(false);
      });
      this.client.on("error", (err) => {
        //log.write("LOG_ERR", "PubClient Error: " + err.message);
      });
      this.client.on("packetreceive", (packet) => {
        //log.write("LOG_INFO", "PubClient packet received: " + packet.cmd);
      });
    }
  }

  publish(topic, qos, msg) {
    if(this.client !== undefined) {
      this.client.publish(topic, msg, { qos: parseInt(qos), retain: true });
      //log.write("LOG_INFO", "Publishing Message with: topic: " + topic + " payload: " + msg);
    } else {
      log.write("LOG_ERR", "Error: Publish not possible because client undefined");
    }
  }

  close() {
    if(this.client !== undefined) {
      this.client.end();
      log.write("LOG_INFO", "PubClient closing");
    }
  }
}

You can simplify it, using local variables.您可以使用局部变量来简化它。 When work is done can assign to this.工作完成后可以分配给这个。 You can also use restructure to use the variables.您还可以使用重组来使用变量。

Local Var局部变量

constructor() {
  const client = mqtt.connect("mqtts://" + host, options); // mqtts for tls connection

  client.on("close", () => {
    //log.write("LOG_INFO", "PubClient close");
  });

  this.client = client;
}

De-Structure:解构:

doSomthingOnClient() {
  const {client} = this // destrcuture
  client.get() // use it 
}

Whole class:整个 class:

class Client {
  constructor() {
    const client = mqtt.connect("mqtts://" + host, options); // mqtts for tls connection
    client.on("connect", () => {
      log.write(
        "LOG_INFO",
        "PubClient connected to Broker: " + host + ":" + settings.port
      );
      client.publish("client/publisher/status", "online", {
        qos: 1,
        retain: true,
      });
      clientToSocket.pubConnect(true);
    });
    client.on("close", () => {
      //log.write("LOG_INFO", "PubClient close");
    });
    client.on("disconnect", (packet) => {
      log.write(
        "LOG_INFO",
        "PubClient disconnected. Reasoncode: " + packet.reasoncode
      );
    });
    this.client = client;
  }
  getClient() {
    return this.client;
  }
  doSomthingOnClient() {
    const {client} = this // destrcuture
    client.get() // use it 
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM