I am rather confused with mechanics and hierarchy of things in javascript, reading few links and pages are adding to them.
There are various ways to create objects in Javascript. What I am getting confused is in following:
I have 3 websocket say WSServiceA, WSServiceB, WSServiceC : These are 3 different sockets and provide different data but data structure is same. Using a simple function I am updating 3 tables in web-page.
Each of these have onopen, onclose and onmessage function so I have simply copied same code 3 times, like this:
WSServiceA.onmessage(){}
WSServiceB.onmessage(){}
WSServiceC.onmessage(){}
If I create a something like this:
function CreateService(Service, WSAddress){
this.service=Service;
this.WSAddress = WSAddress;
this.websocket = new WebSocket(this.WSAddres);
this.onopen = function(event){ Send some message }
this.onmessage = function(event){ Update Table A}
}
After that I will create
ServiceA = CreateService("A", "ws://192.168.100.1.1:10001");
ServiceB = CreateService("B", "ws://192.168.100.1.2:10001");
ServiceC = CreateService("C", "ws://192.168.100.1.3:10001");
Then how and who will call ServiceA.onopen, Service.onmessage and other functions - when data comes to those sockets? Will it be called at all? Something created inside function should be visible only in that function - I am not able to understand flow and link. Probably I am missing some key concept or this should not work.