1

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.

Kalreg
  • 792
  • 1
  • 8
  • 22
  • That's because if you use `this` inside `chrome.serial.connect()`, it is relative to `chrome.serial`, not to `station`. See http://stackoverflow.com/questions/337878/var-self-this – Michał Perłakowski Feb 07 '16 at 11:23
  • so i've expected that's the reason but still your link sets variable that is one in whole code, i need variable that is unique one particular object, and it should be public so i have to name it with this. – Kalreg Feb 07 '16 at 11:30
  • I think you can solve this by defining a global array of integers, and then associate each member of this array with each new station() object that is opened, and use this global array as means of communications back and forth to this instance – Miki Berkovich Feb 07 '16 at 11:34
  • yes i can but it doesnt seem to me like object oriented programming anymore:( especially that i am loosing my instance number (i) while digging into next function so i cant event know which number of station i am working with – Kalreg Feb 07 '16 at 11:37
  • That's because the `function(connInfo)` agument in the chrome.serial.connect's call has a scope of its own. Have you tried adding another method to your "station" object, say `this.setPort=function(port){this.connectionInfo=port}` and then put that function as an argument for chrome.serial.connect instead? like: `chrome.serial.connect(port, {bitrate: 9600 }, this.setPort)` ? – Miki Berkovich Feb 07 '16 at 11:43
  • i dont see how it can work. this.setPort doesnt set property value of connectionInfo to station[0], station[1] if i put it to my code :( `this.connectionInfo = 25; this.setPort = function(port) { this.connectionInfo = port; console.log(stations[0].connectionInfo)} this.connect = function (port) { chrome.serial.connect(port, {bitrate: game.baud}, this.setPort) }` Code above returns 25 so it doesnt change it :( – Kalreg Feb 07 '16 at 12:02
  • Hmm.. yes you're right. I checked it myself too. If there's no OOP solution to this, then I guess that a global array is your only option then if you want to make it work. I know that in C++ or in PHP for example you can pass a reference (or pointer) to an outside object. I thought that passing "this.setport" as an argument to a function was supposed to do the job. Perhaps you should look more into this approach, I think this should be the general way to solve it. – Miki Berkovich Feb 07 '16 at 12:48

0 Answers0