I am trying to create a class for serial port using Node.js, but I want to know if there is a better way to write my data class code.
In my sample code below, in the getPortName
prototype in the forEach
loop, I declare a port object to store the serial port information before pushing into an array.
Is this a good way of declaring this port object? I think it is not good because it is difficult for users to see what the objects' members contain. Is there any way to make it clearly expose the port object member? Or is there a better way to improve this code structure?
'use strict';
var serialPort = require("serialport");
function Serial() {
this.ComName = "";
}
Serial.prototype.getPortName = function() {
serialPort.list(function (err, ports) {
var availablePorts = [];
ports.forEach(function(port) {
var port = {
comName: "",
manufacturer: "",
pnpId:
}
port.comName = port.comName;
port.manufacturer = port.manufacturer;
port.pnpId = port.pnpId;
availablePorts.push(port);
});
});
return availablePorts;
};
Serial.prototype.setCOMName = function(name) {
this.ComName = name;
}
module.exports = Serial;