I’m trying to figure out a way to create a new class instance when values are passed from an Android app to a JavaScript program. I know JS doesn’t use classes, but there are ways to use functions in a similar way.
When a user presses a button in the Android app, I want to send a randomly generated ID (which I have created) to a database that stores the ID (and user info), and then have a JS program pull the ID and create a new instance for that ID. There will be multiple users accessing this at the same time, so I need to create a new instance each time a user presses the button. It will also send the ID, latitude, longitude, and time to a separate database where the location will be updated and stored every second.
For example, if ‘user1’ presses the button, the id (user1) will be sent to DB_1 and the ID, latitude, longitude, and timestamp will be sent to DB_2. The JS will be alerted of the new user and create an instance for user1, and then will use the ID as a variable to search DB_2 for coordinates every second. This way, if there are multiple users, each class will only search DB_2 for coordinates that pertain to that user. I need to do this because I must be able to track multiple users at the same time (and in real time) with a Google Map on the web. Let me know if you have any suggestions!
Thanks
If all you want to do is make a JavaScript object with it’s own private variables then try something like
You can then use prototype functions.
Now you have an object with a private variable id and a getter for that variable.
You can create instances of the object like this
I like to use the prototype pattern because it keeps things safe and organized but you do end up using “this” a lot. There are other patterns that achieve basically the same thing, which you may prefer to use but I can’t think if the name of any of them right now.
In the construct using
Is sort of similar to doing this in Java except without type safety.
Of course there is a pretty big difference between the inner workings of compiled Java and interpreted JavaScript but the above method gives you a similar OO effect.
If you use this method and end up with some errors, in all probability you have left out a “this” somewhere. Every variable in the prototype function that is part of the object must have the “this.” prefix.
Hope that’s something along the lines of what you were looking for.