I've been stuck for a while on that issue that i even have not an idea what is wrong since for me it should work perfectly. I use chrome and javascript to dynamically create object of class "station". My code looks like this:
var stations = {};
function station() {
this.connectionInfo = 25;
this.connect = function (port) {
chrome.serial.connect(port, {bitrate: 9600 }, function (connInfo) {
this.connectionInfo = connInfo;
console.log('Port has been opened!')
}
}
}
Connect function is started from object "game".
function game() {
[...]
this.connectToPorts = function () {
for ( var i = 0; i < game.deviceCounter; i++ ) {
var port = game.serialPorts[i];
if( port.length > 0 ){
stations[i] = new station();
stations[i].connect(port);
}
}
}
}
I want each "station" to be separated object in game that contains its properties like connectionInfo which differs between stations. Because i have changable ammount of stations i create them dynamically with stations[i] in game object. But in process of creating it i want also to have unique connection id in each object.
PROBLEM: this.connectionInfo in station[i] is undefined all the time. I cant read it after setting it via station[i].connectionInfo. Also this.connectionInfo is limited to this scope and i cant resolve it from other methods in station object. Please mention that callback function of chrome.serial.connect uses as argument connInfo but i can (or i dont know how) to pass number of object (i) so i can set it like stations[i].connectionInfo. I mean
chrome.serial.connect(port, {bitrate: 9600 }, function (connInfo, i = 25) {}
I guess the problem lies somewhere in inheritance and i set this.connectionInfo with this chrome.serial.connect instead of stations object but have no idea what to do to solve it. Any help would be appreciated.