如何用 this 调用调用者命名空间的函数?
目标:我试图通过使用“this”而不是命名空间字符串来缩短在函数中调用同一命名空间的函数的方式。但显然“this”不适用于函数,它只适用于变量。我想知道这是否是由于我的误用造成的,或者使用命名空间字符串调用函数是唯一的方法。
另一个初学者的问题:我正在调用与调用者具有相同命名空间的函数。我用谷歌搜索,有人通过调用说: this.myfunction 可以工作,但事实并非如此。有什么想法吗?谢谢。
我打电话: singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());
来自: index.fragment.singleEntry.editContent.load
我尝试这样做:this.accept,但firebug说this.accept不是一个函数...
index.fragment.singleEntry.editContent.load = (function(singleEntry) {
return function() {
// prepare edit dialog
$("#editContentDiv").dialog({
autoOpen : false,
height : 500,
width : 600,
modal : true,
buttons : {
"OK" : function() {
singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());
$(this).dialog("close");
},
"CANCEL" : function() {
$(this).dialog("close");
}
},
open : function() {
// set value to the edit area
tinyMCE.get("editContentTa").setContent($("#veContent").html());
}
});
Goal: I'm trying to shorten how I call a function of the same namespace in a function by using "this" instead of the namespace string. But apparently "this" wouldn't work for a function, it only works for a variable. I wish to know if it's due to my misusage, or to call function with a namespace string is the only way.
Another beginner's question: I'm calling a function of the same namespace as the caller's. I googled and someone said by calling: this.myfunction would work, but it doesn't. Any ideas? thanks.
I'm calling:
singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());
from:
index.fragment.singleEntry.editContent.load
I've tried to do it as: this.accept, but firebug says this.accept is not a function...
index.fragment.singleEntry.editContent.load = (function(singleEntry) {
return function() {
// prepare edit dialog
$("#editContentDiv").dialog({
autoOpen : false,
height : 500,
width : 600,
modal : true,
buttons : {
"OK" : function() {
singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());
$(this).dialog("close");
},
"CANCEL" : function() {
$(this).dialog("close");
}
},
open : function() {
// set value to the edit area
tinyMCE.get("editContentTa").setContent($("#veContent").html());
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我并没有完全遵循您的最终目标,因此一些理论希望它可以帮助您实现目标:
当您通过对象的属性调用函数时(因此,通过点分符号或
[]
表示法),函数调用中的this
将是对象引用。因此,当您在该调用中调用 ...时,
this
将引用singleEntry.editContent
。如果您希望
this
引用其他内容,可以使用call
:...其中
thisObj
是您希望函数看到的任何对象在通话期间作为this
。更多信息请参见:神话方法call
是所有(真实)JavaScript 函数的一个属性,专门提供它以便于调用函数并设置this
应该是什么(上下文) em> 是)在通话期间。还有apply
,它执行完全相同的操作,但接受参数作为数组而不是离散参数传递给函数。例如,这两个调用是相同的:所有这一切之所以成为可能,是因为在 JavaScript 中,
this
完全取决于如何调用函数,而不是定义它的位置 ——根据你的问题,我想你知道这一点,但无论如何还是值得标记。I'm not quite following your ultimate goal, so some theory in hopes it helps you get there:
When you call a function via an object's property (so, via dotted notation, or
[]
notation),this
within the function call will be the object reference. So when you call...within that call,
this
will refer tosingleEntry.editContent
.If you want
this
to refer to something else, you can usecall
:...where
thisObj
is whatever object you want the function to see asthis
during the call. More here: Mythical methodscall
is a property of all (real) JavaScript functions, and is provide specifically to make it easy to call the function and set whatthis
should be (what the context is) during the call. There's alsoapply
, which does exactly the same thing but accepts the arguments to pass to the function as an array rather that as discrete arguments. E.g., these two calls are identical:All of this is made possible because in JavaScript,
this
is determined entirely by how a function is called, not where it's defined — I think you know that, based on your question, but worth flagging up anyway.