创建新的“类”;在 JavaScript 中调用时的实例

发布于 2024-12-29 12:51:55 字数 501 浏览 1 评论 0原文

我试图找出一种方法,当值从 Android 应用程序传递到 JavaScript 程序时创建新的类实例。我知道 JS 不使用类,但是有一些方法可以以类似的方式使用函数。

当用户按下 Android 应用程序中的按钮时,我想将随机生成的 ID(我创建的)发送到存储 ID(和用户信息)的数据库,然后让 JS 程序提取 ID 并创建一个该 ID 的新实例。将会有多个用户同时访问它,因此每次用户按下按钮时我都需要创建一个新实例。它还会将 ID、纬度、经度和时间发送到一个单独的数据库,其中位置每秒都会更新和存储。

例如,如果“user1”按下按钮,则 id(user1)将被发送到 DB_1,ID、纬度、经度和时间戳将被发送到 DB_2。 JS 会收到新用户的警报,并为 user1 创建一个实例,然后使用 ID 作为变量,每秒在 DB_2 中搜索坐标。这样,如果有多个用户,每个类将仅在 DB_2 中搜索属于该用户的坐标。我需要这样做,因为我必须能够使用网络上的 Google 地图同时(实时)跟踪多个用户。如果您有任何建议,请告诉我!

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2025-01-05 12:51:55

如果您想做的只是使用自己的私有变量创建一个 JavaScript 对象,那么请尝试类似

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

您可以使用原型函数。

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

现在你有了一个带有私有变量 id 和该变量的 getter 的对象。

您可以像这样创建对象的实例,

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

我喜欢使用原型模式,因为它可以保持事物的安全性和组织性,但您最终会经常使用“this”。还有其他模式可以实现基本相同的功能,您可能更喜欢使用它们,但我现在不知道其中任何一个的名称。

在构造中使用

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

Is 与在 Java 中执行此操作有点类似,只是没有类型安全。

public class User{
    private int id;

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

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

当然,编译的 Java 和解释的 JavaScript 的内部工作方式之间存在相当大的差异,但上述方法为您提供了类似的 OO 效果。

如果您使用此方法并最终出现一些错误,则很可能您在某处遗漏了“this”。原型函数中作为对象一部分的每个变量都必须具有“this”。前缀。

希望这符合您正在寻找的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文