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());
}
});
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),thiswithin the function call will be the object reference. So when you call…within that call,
thiswill refer tosingleEntry.editContent.If you want
thisto refer to something else, you can usecall:…where
thisObjis whatever object you want the function to see asthisduring the call. More here: Mythical methodscallis a property of all (real) JavaScript functions, and is provide specifically to make it easy to call the function and set whatthisshould 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,
thisis 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.