Strophe에 참여할 각 방의 대상이 있습니다. 이 객체에는이 특정 회의실에 대한 현재 스탠자를 처리하는 기능이 있습니다. mainRoom.presenceHandler()
기능이 기능에 좌향 선회, this
하여 D 자에 의해 호출 될 때Strophe 스탠자 핸들러의이 참조
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();
은 그러나 더 이상 스탠자 자체가 아니라 mainRoom
에를 말한다, 그래서 mainRoom
에서 속성에 액세스 할 수 없습니다.
presenceHandler 함수 내에서 room 객체의 속성에 어떻게 액세스 할 수 있습니까? 핸들러이
var thiss=this;
this.join = function() {
connection.addHandler(function(presence)
{thiss.presenceHandler(presence);},null,"presence",null,null,this.name);
connection.send(/*presence*/);
}
메모와 함께 코드 위의 폐쇄를 교체
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
}
}