mkyong
mkyong

Reputation: 12947

Creating new 'class' instance when called in JavaScript

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

Upvotes: 1

Views: 487

Answers (1)

Ruairi O'Brien
Ruairi O'Brien

Reputation: 1229

If all you want to do is make a JavaScript object with it's own private variables then try something like

function User(id) {
this.id = id;
};

You can then use prototype functions.

User.prototype.getId = function() {
return this.Id;
};

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

var user1 = new User(id1);
var user2 = new User(id2);
var user1Id = user1.getId();
var user2Id = user2.getId();

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

function User(id) {
this.id = id;
};
User.prototype.getId = function() {
return this.Id;
};

Is sort of similar to doing this in Java except without type safety.

public class User{
    private int id;

    public User(int id){
         this.id = id;
    }

    public int getUserId(){
        return this.id;
    }
}

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.

Upvotes: 1

Related Questions