【发布时间】:2025-11-24 02:25:01
【问题描述】:
简短描述:我正在使用带有函数声明、新关键字和原型方法的 OO Javascript(示例如下)。我需要一种方法来引用对象的每个方法中的“自我”对象。 “this”似乎只有在我直接调用该方法时才有效,否则“this”似乎指的是调用该方法的任何内容。
更多细节:这是我的对象的简化版本。
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
我尽量简化代码,并在 cmets 中解释问题。我为第一个场景(调用 this.myCallback)创建了一个解决方法,方法是创建 this 的本地副本并使回调参数成为匿名函数,该函数使用我的本地副本调用我需要的函数。但第二种情况更麻烦(事件触发)。
我很好奇是否存在不同的面向对象编程模型,其中每个方法总是对父对象具有正确的引用,而不管方法是如何被调用的?或者如果有不同的方法来创建一个对象作为自定义对象的属性并将其事件绑定到自定义对象的方法?对不起,如果这是令人困惑的语言!请注意,我的问题与 RTCPeerConnection 无关,恰好是我目前正在从事的项目。
我找到了这篇文章:http://www.alistapart.com/articles/getoutbindingsituations,但我不完全确定如何使用这些信息?
【问题讨论】:
标签: javascript oop