You are reading a single comment by @d0773d and its replies. Click here to read the full conversation.
  • The structure looks pretty tidy - although I'd really recommend doing:

    Sensor.prototype.getSensorType = function ...
    Sensor.prototype.getSensorAddress = function ...
    // etc
    

    Instead of your Sensor.prototype = {. It's probably more personal opinion, but it seems more pleasant than having to redefine constructor.

    You've also defined getters like getSensorAddress, which is fine - however you then do a = this.getSensorAddress;. That'll return the function itself, rather than executing it and getting the result... You might also want to put a var before a, so a isn't defined as a global variable.

    In your code getSensorResult returns immediately, so while you get a value in getSensorReading you're not actually returning it. For that, you'd need to use a callback:

      getSensorResult:function (callback) {
        // ...
        setTimeout(function (e) { callback(that.getSensorReading()); }, w);
      },
    

    Then you can use it with:

    ph.getSensorResult(function(value) {
      console.log(value);
    });
    

    Hope that helps!

  • Hi @Gordon The reason why I chose Sensor.prototype = { was, at the time, I didn't care for the repetitiveness of writing Sensor.prototype. However, thats nothing that copy and paste can't fix :). As you recommended, I switched to Sensor.prototype.getSensorType = function. I can appreciate the readability and the code seems to flow a little better and makes sense to me.

About

Avatar for d0773d @d0773d started