Javascript OO参考了这个 [英] Javascript OO reference this

查看:74
本文介绍了Javascript OO参考了这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短描述:我正在使用OO Javascript和函数声明,新关键字和原型方法(下面的示例)。我需要一种方法来引用对象的每个方法中的self对象。 这个似乎只有在我直接调用方法时才有效,否则this似乎指的是所谓的方法。

Short description: I am using OO Javascript with the function declaration, new keyword, and prototype approach (example below). I need a way to reference the "self" object within each method of the object. "this" seems to only work if I am calling the method directly, otherwise "this" seems to refer to whatever called the method instead.

更多细节:这是一个我的对象的简化版本。

More details: Here is a simplified version of my object.

function Peer(id) {
    this.id = id;
    this.pc = new RTCPeerConnection(SERVER);

    this.pc.onevent1 = this.onEvent1;
    this.pc.onevent2 = this.onEvent2;
}

Peer.prototype.doSomething = function() {
    // create offer takes one param, a callback function
    this.pc.createOffer(this.myCallback);
};

Peer.prototype.onEvent1 = function(evt) {
    // here this refers to the RTCPeerConnection object, so this doesn't work
    this.initSomething(evt.data);    
};

Peer.prototype.myCallback = function(data) {
    // here this refers to the window object, so this doesn't work
    this.setSomething = data;
};

Peer.prototype.initSomething = function(data) {
    // use data
};

以下是使用它的示例。

var peer = new Peer(1);
peer.doSomething();
// eventually something triggers event1

我试图尽可能地简化代码在评论中解释问题。我为第一个场景(调用this.myCallback)创建了一个解决方法,创建了一个本地副本,并使回调参数成为一个匿名函数,使用我的本地副本调用我需要的函数。但第二种情况更麻烦(事件发生)。

I tried to simplify the code as much as possible and explain the problem in comments. I created a workaround for the first scenario (calling this.myCallback) by creating a local copy of this and making the callback param an anonymous function that calls the function I need using my local copy of this. But the second scenario is more troublesome (an event firing).

我很好奇,如果有一个不同的面向对象编程模型,其中每个方法总是有一个适当的参考到父对象,无论调用该方法如何?或者,如果有不同的方法将对象创建为自定义对象的属性并将其事件绑定到自定义对象的方法?对不起,如果这是令人困惑的语言!请注意,我的问题与RTCPeerConnection无关,恰好是我正在处理的项目。

I was curious if there is a different model for Object Oriented programming in which every method always has a proper reference to the parent object, regardless of how the method was invoked? Or if there is a different way to create an object as a property of a custom object and bind its events to the custom object's method? Sorry if this is confusing language! Note that my question has nothing to do with RTCPeerConnection, that just happened to be the project I'm working on currently.

我发现这篇文章: http://www.alistapart.com/articles/getoutbindingsituations 但我不完全确定如何使用这些信息?

I found this article: http://www.alistapart.com/articles/getoutbindingsituations but I wasn't totally sure how to use this information?

推荐答案


我很好奇是否有面向对象编程的不同模型,其中每个方法总是有一个对父对象的正确引用,无论该方法是如何被调用的?

I was curious if there is a different model for Object Oriented programming in which every method always has a proper reference to the parent object, regardless of how the method was invoked?

是的,它有:它被称为 Function.bind

Yes, there is: it's called Function.bind.

function Peer(id) {
    this.id = id;
    this.pc = new RTCPeerConnection(SERVER);

    this.pc.onevent1 = this.onEvent1.bind(this);
    this.pc.onevent2 = this.onEvent2.bind(this);
}

同样,对于 myCallback 总是引用 Peer 实例:

Likewise, for myCallback's this to always reference the Peer instance:

Peer.prototype.doSomething = function() {
    // create offer takes one param, a callback function
    this.pc.createOffer(this.myCallback.bind(this));
};

这篇关于Javascript OO参考了这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆