如何在 Javascript 中获取对象的正确上下文
How to get the right context of the object in Javascript
我有一个 class,名字是 TestClass
。它有一个 TooltipDialog
对象成员和一个 testFunc
函数。
在ToolTipDialog
中,它有单选按钮。
预计当用户单击单选按钮时,它会执行 testFunc
.
我的问题是:当用户单击单选按钮时,如何获取父上下文 (TestClass
)?
可以执行类似 TestClass.testFunc()
.
的操作
当我尝试使用 testFunc
时,错误是:testFunc is not defined
当我尝试使用 this.testFunc
时,错误是 this.testFunc is not a function
TestClass
的代码如下,Fiddlelink是js code
define(["dojo/_base/declare","dojo/dom","dojo/on","dojo/_base/lang","dijit/registry",
"dijit/TooltipDialog","dijit/popup","esri/InfoWindowBase",
"dojo/_base/array"],
function (declare,dom,on,lang,registry,TooltipDialog,popup,InfoWindowBase,Array) {
return declare(
InfoWindowBase,
{
constructor: function (parameters) {
//lang.mixin(this, parameters);
this.son = "";
},
//member of the ParentClass
init: function ( ) {
var tpContent = '<div id="dp1"><label for="name">first:</label> <input type="radio" id="first" checked value="pri"/>' +
'<br><label for="hobby"> second:</label> <input type="radio" id="second" value="sec"/></div>';
this.inherited(arguments);
this.son = new TooltipDialog({
content: tpContent,
onMouseLeave: function (e) {
}
,
onOpen: function (e) {
var sHu=dom.byId("first");
on(sHu,"click", function(){
// this.testFunc();
});
},
});
popup.open({
popup: this.son,
orient: ["above","below"],
around: dom.byId("tpHolder")
});
},
testFunc:function(){
console.log("testFunc");
}
}
);
});
事件的上下文将始终是事件发生的 control/node,在您的情况下它将是对象:sHu。您可以使用 lang.hitch 方法更改上下文。
on(sHu,"click", lang.hitch(this, function(){
this.testFunc();
}));
不需要传递this
,您可以更改为您想要作为函数上下文的任何对象。
正如@T Kambi 所说,您需要使用 hitch
。
但你的情况,你需要双 hitch
:
onOpen: lang.hitch(this, function (e) {
var sHu=dom.byId("first");
on(sHu,"click", lang.hitch(this, function(){
this.testFunc();
}));
}),
第一个hitch
将使this
成为onOpen
方法中的TestClass
,第二个hitch
将使this
onOpen
方法是 click
处理程序的 this
。
我修复了 JSFiddle,所以你看看:https://fiddle.jshell.net/etxx1o0s/5/
我有一个 class,名字是 TestClass
。它有一个 TooltipDialog
对象成员和一个 testFunc
函数。
在ToolTipDialog
中,它有单选按钮。
预计当用户单击单选按钮时,它会执行 testFunc
.
我的问题是:当用户单击单选按钮时,如何获取父上下文 (TestClass
)?
可以执行类似 TestClass.testFunc()
.
的操作
当我尝试使用 testFunc
时,错误是:testFunc is not defined
当我尝试使用 this.testFunc
时,错误是 this.testFunc is not a function
TestClass
的代码如下,Fiddlelink是js code
define(["dojo/_base/declare","dojo/dom","dojo/on","dojo/_base/lang","dijit/registry",
"dijit/TooltipDialog","dijit/popup","esri/InfoWindowBase",
"dojo/_base/array"],
function (declare,dom,on,lang,registry,TooltipDialog,popup,InfoWindowBase,Array) {
return declare(
InfoWindowBase,
{
constructor: function (parameters) {
//lang.mixin(this, parameters);
this.son = "";
},
//member of the ParentClass
init: function ( ) {
var tpContent = '<div id="dp1"><label for="name">first:</label> <input type="radio" id="first" checked value="pri"/>' +
'<br><label for="hobby"> second:</label> <input type="radio" id="second" value="sec"/></div>';
this.inherited(arguments);
this.son = new TooltipDialog({
content: tpContent,
onMouseLeave: function (e) {
}
,
onOpen: function (e) {
var sHu=dom.byId("first");
on(sHu,"click", function(){
// this.testFunc();
});
},
});
popup.open({
popup: this.son,
orient: ["above","below"],
around: dom.byId("tpHolder")
});
},
testFunc:function(){
console.log("testFunc");
}
}
);
});
事件的上下文将始终是事件发生的 control/node,在您的情况下它将是对象:sHu。您可以使用 lang.hitch 方法更改上下文。
on(sHu,"click", lang.hitch(this, function(){
this.testFunc();
}));
不需要传递this
,您可以更改为您想要作为函数上下文的任何对象。
正如@T Kambi 所说,您需要使用 hitch
。
但你的情况,你需要双 hitch
:
onOpen: lang.hitch(this, function (e) {
var sHu=dom.byId("first");
on(sHu,"click", lang.hitch(this, function(){
this.testFunc();
}));
}),
第一个hitch
将使this
成为onOpen
方法中的TestClass
,第二个hitch
将使this
onOpen
方法是 click
处理程序的 this
。
我修复了 JSFiddle,所以你看看:https://fiddle.jshell.net/etxx1o0s/5/