The following code is supposed to abstract a temperature sensor for a project involving Arduino.
I have the Sensor function representing the hardware sensor, a sensorFactory function to create instances of the sensor, and a saveSensor / findSensor function that stores / returns the sensor objects from an array.
var sensors = [];
const Sensor = (sensorId, sensorType, sensorName) =>{
var temperature;
let sensor = {
sensorId,
sensorType,
sensorName,
temperature,
readTemperature: (temp) => {
temperature = temp //simulates reading from hardware
},
getTemperature: () => {
console.log(temperature)
}
}
return sensor;
};
const SensorFactory = () =>({
createSensor: (sensorId, sensorType, sensorName) => {
var sensor = {};
switch(sensorType){
case "DS18B20":
sensor = Sensor(sensorId,sensorType, sensorName);
saveSensor(sensor);
return findSensor(sensorId);
break;
}
},
getSensor: (sensorId) =>{
if (sensorId){
return findSensor(sensorId);
}
return sensors;
}
});
const saveSensor = (sensor) => {
sensors.push(sensor);
};
const findSensor = (sensorId) => {
return sensors[sensorId -1];
};
module.exports = SensorFactory;
I have two questions:
Why can't I see the value of the temperature variable when I run the line
console.log(JSON.stringify(sensor1))?const SensorFactory = require ("./sensor.js"); var sensorFactory = SensorFactory(); sensorFactory.createSensor (1,"DS18B20", "Cryogenic Tank"); var sensor1 = sensorFactory.getSensor(1); sensor1.readTemperature(200); sensor1.getTemperature(); //prints 200 sensor1.readTemperature(100); //prints 100 sensor1.getTemperature(); sensor1.readTemperature(10); //prints 10 sensor1.getTemperature(); console.log(JSON.stringify(sensor1))
Expected:
{"sensorId":1,"sensorType":"DS18B20","sensorName":"Cryogenic Tank", "temperature": 10}
Actual output:
{"sensorId":1,"sensorType":"DS18B20","sensorName":"Cryogenic Tank"}
I believe that temperature (A) in the following block is a private variable so it should not be accessible from outside the object, but shouldn't I be able to see the temperature variable (B) when I run console.log(JSON.stringify(sensor1))?
const Sensor = (sensorId, sensorType, sensorName) =>{
var temperature; <----A----private.
let sensor = {
sensorId,
sensorType,
sensorName,
temperature, <----B----I should be able to see this, right?
readTemperature: (temp) => {
temperature = temp;
console.log('readTemperature');
},
getTemperature: () => {
console.log('getTemperature');
console.log(temperature); }
}
return sensor;
};
- I am very rusty with JS. Can you please help me understand where I am conceptually wrong? How would you modify the code so that it works as expected?
SensorFactorya function that returns a factory object? It would seem that all factories are the same. Just usemodule.exports = {createSensor(…) { … }, getSensor(…) { … }};JSON.stringifyonly prints the value of the.temperatureobject property, not thetemperaturelocal variable that yourreadTemperaturemethod assigns. UsegetTemperature()instead and remove the immutable property from your object - or remove the variable from your code and always access the property.return temperatureline in thegetTemperaturemethod. My code works now as intended. Can you help me understand what do you mean when you say "Why is SensorFactory a function that returns a factory object?" My idea is to have a SensorFactory returning different types of sensor objects.sensorFactorydoes with itscreateSensormethod. But why do you have aSensorFactoryfunction to create the factory?