Strope 节处理程序中的此引用

发布于 2024-12-03 15:49:10 字数 768 浏览 0 评论 0原文

我为每个要加入 Strope 的房间准备了一个对象。该对象包含一个用于处理该特定房间的存在节的函数。

function Room(name, someData)
    this.name = name;
    this.someData = someData;

    this.presenceHandler = function(presence) {
        console.log(this.name, this.someData);
    }

    this.join = function() {
        connection.addHandler(this.presenceHandler,null,"presence",null,null,this.name);
        connection.send(/*presence*/);
    }
}

var connection = new Strophe.Connection(/*http-bind*/);
var mainRoom = new Room("main", {foo: "bar"});
mainRoom.join();

但是,当 Strope 的节调用 mainRoom.presenceHandler() 函数时,函数中的 this 指的是节本身,而不是 mainRoom 不再存在,所以我无法从 mainRoom 访问属性。

您能告诉我,如何从 PresenceHandler 函数中访问房间对象的属性吗?

I have an object for each room I'm joining with Strophe. This object contains a function for handling presence stanzas for this particular room.

function Room(name, someData)
    this.name = name;
    this.someData = someData;

    this.presenceHandler = function(presence) {
        console.log(this.name, this.someData);
    }

    this.join = function() {
        connection.addHandler(this.presenceHandler,null,"presence",null,null,this.name);
        connection.send(/*presence*/);
    }
}

var connection = new Strophe.Connection(/*http-bind*/);
var mainRoom = new Room("main", {foo: "bar"});
mainRoom.join();

But when the mainRoom.presenceHandler() function is called by an stanza by Strophe, this in the function refers to the stanza itself and not to mainRoom anymore, so I cannot access the attributes from mainRoom.

Could you tell me, how I can access the attributes of the room object from within the presenceHandler function?

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

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

发布评论

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

评论(2

远山浅 2024-12-10 15:49:10

尝试在函数内再次初始化主类...

function MainFunc() {

  this.method1 = function() {
    this.property1 = "foo";
  }

  this.method2 = function() {
   var parent = this; // assign the main function to a variable.
   parent.property2 = "bar"; // you can access the main function. using the variable
  }
}

try initializing the main class again inside the function ...

function MainFunc() {

  this.method1 = function() {
    this.property1 = "foo";
  }

  this.method2 = function() {
   var parent = this; // assign the main function to a variable.
   parent.property2 = "bar"; // you can access the main function. using the variable
  }
}
仄言 2024-12-10 15:49:10
        this.join = function() {
    connection.addHandler(this.presenceHandler,null,"presence",null,null,this.name);
    connection.send(/*presence*/);
}

将上面的代码替换为

        var thiss=this;
    this.join = function() {
    connection.addHandler(function(presence)             
    {thiss.presenceHandler(presence);},null,"presence",null,null,this.name);
    connection.send(/*presence*/);
}

处理程序的闭包注释

        this.join = function() {
    connection.addHandler(this.presenceHandler,null,"presence",null,null,this.name);
    connection.send(/*presence*/);
}

Replace above code with this

        var thiss=this;
    this.join = function() {
    connection.addHandler(function(presence)             
    {thiss.presenceHandler(presence);},null,"presence",null,null,this.name);
    connection.send(/*presence*/);
}

note the closures for the handler

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