如何访问本机对象调用的函数中的实例?

发布于 2024-10-17 06:20:21 字数 651 浏览 3 评论 0原文

我承认我已经问过有关 this 关键字及其在某些情况下的含义的问题,但恐怕我仍然没有完全理解它。任何提示将不胜感激。

我基本上有一个 Socket 类,它是 HTML5 的 WebSocket 类的包装器,以使事情变得更容易(我只复制了与我的问题相关的内容):

var Socket = function(url) {
    this.url = url;
    this.webSocket = null;
}

Socket.prototype.init = function() {
    this.webSocket = new WebSocket(this.url);
    this.webSocket.onmessage = this.parseMessage;
};

Socket.prototype.parseMessage = function(event) {
    console.log(this); // logs the WebSocket (native) instance
}

在 parseMessage 函数中, this 指的是实例本地 WebSocket 对象的实例,那么我如何在这里访问我的 Socket 对象的实例呢?

多谢。

I admit that I've already asked a question about the this keyword and what it means in certain situations, but I still do not completely understand it I'm afraid. Any hints would be greatly appreciated.

I basically have a Socket class, which is a wrapper to the WebSocket class of HTML5 to make things easier (I only copied what is relevant to my question):

var Socket = function(url) {
    this.url = url;
    this.webSocket = null;
}

Socket.prototype.init = function() {
    this.webSocket = new WebSocket(this.url);
    this.webSocket.onmessage = this.parseMessage;
};

Socket.prototype.parseMessage = function(event) {
    console.log(this); // logs the WebSocket (native) instance
}

In the parseMessage function, this refers to the instance of the native WebSocket object, so how can I access the instance to my Socket object here?

Thanks a lot.

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

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

发布评论

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

评论(1

盛夏已如深秋| 2024-10-24 06:20:21

您可以绑定它:

this.webSocket.onmessage = this.parseMessage.bind(this);

但是,并非所有浏览器本身都支持此功能,但您可以从 MDC 使所有浏览器兼容。

如果您不熟悉绑定...它可以让您具体指定调用该函数时this应该是什么。当该行运行时,this 是您的 Socket 类实例,因此您基本上是强制 this inside parseMessage 指向回该实例。

如果由于某种原因,您不想使用 bind,另一种选择是将 this 的副本保存到变量中,这样事件中的含义就不会丢失派遣:

Socket.prototype.init = function() {
    this.webSocket = new WebSocket(this.url);
    var _self = this;
    this.webSocket.onmessage = function (event) {
        _self.parseMessage.apply(_self, [event]);
    };
};

You can bind it:

this.webSocket.onmessage = this.parseMessage.bind(this);

However, not all browsers support this natively, but you can grab a code snippet from MDC to make all browsers compatible.

In you're unfamiliar with binding ... it lets you specify specifically what this should be when that function is called. At the time that line runs, this is your Socket class instance, so you're basically forcing this inside parseMessage to point back to the instance.

If, for some reason, you don't want to use bind, another option is to save a copy of this to a variable so the meaning is not lost inside an event dispatch:

Socket.prototype.init = function() {
    this.webSocket = new WebSocket(this.url);
    var _self = this;
    this.webSocket.onmessage = function (event) {
        _self.parseMessage.apply(_self, [event]);
    };
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文