MJ
asked on
Having Returning Anonymous Function Access Global Function
I have no idea how to get an anonymous function that returns a value to source a global function? If I have a normal anonymous function, everything works fine. If the anonymous function returns, then it doesn't work? Is there any way to get this to work? Example:
/ /global function
function _myGlobalFunct(str){
return (str);
}
the following anonymous function calls global function and it works as expected:// working anonymous function
(function () {
window._myGlobalFunct("value1");
})()
but the following return anonymous function doesn't call my function but doesn't throw any errors? :// Not Working Properly but doesn't throw any errors?
return (function() {
var rvalue = document.title || "No Title";
window._myGlobalFunct(rvalue);
return rvalue;
})();
greetings 894359, , I looked at what you call an "anonymous function" as -
return (function() {
var rvalue = document.title || "No Title";
window._myGlobalFunct(rval ue);
return rvalue;
})();
To me, the problem is when you placed the "return" as the FIRST operation in that progression, I can not see as there is Anything to return "TO", so I can not understand Why you even placed the "return" there at the beginning? ? and you have your "anonymous function" returning a String, Why? ? if you do not use that string on the return, why have a string return?
I have tried this in firefox and it works -
Please notice that I have a return variable as -
r =
for the function, so it can use the result of the text change,
ALSO I have NOT used the window. in-
window._myGlobalFunct(rval ue);
and have it as a more practical -
_myGlobalFunct(rvalue+"!!" );
whay place a window. if it's not needed ?
One last observation, I do not see as using this "anonymous function" is any coding or functional advantage, so why use it?
couldn't you just use -
return (function() {
var rvalue = document.title || "No Title";
window._myGlobalFunct(rval
return rvalue;
})();
To me, the problem is when you placed the "return" as the FIRST operation in that progression, I can not see as there is Anything to return "TO", so I can not understand Why you even placed the "return" there at the beginning? ? and you have your "anonymous function" returning a String, Why? ? if you do not use that string on the return, why have a string return?
I have tried this in firefox and it works -
var r = "";
r = (function() {
var rvalue = document.title || "No Title";
_myGlobalFunct(rvalue+"!!");
return rvalue+"!!";
})();
document.title = r;
Please notice that I have a return variable as -
r =
for the function, so it can use the result of the text change,
ALSO I have NOT used the window. in-
window._myGlobalFunct(rval
and have it as a more practical -
_myGlobalFunct(rvalue+"!!"
whay place a window. if it's not needed ?
One last observation, I do not see as using this "anonymous function" is any coding or functional advantage, so why use it?
couldn't you just use -
var rvalue = (document.title || "No Title")+"!!";
_myGlobalFunct(rvalue);
document.title = rvalue
ASKER
Just a little clarification, these are NOT the exact functions I'm using, so don't focus on the functionality, only getting the global function to be called within the anonymous function. I just used these for clarification and to simplify the issue. In actuality , I'm trying to use a helper function in Adobe DTM. The Anonymous function is actually a bigger function that runs as a data element.
OK, but most any javascript will not run after a -
return
as the first element of the syntax unless there is a "container" to exit the code flow an "return" to the code flow "out of" the container (method or function), so just have the function called as you first have it with out the "return" element, , as -
(function () {
return
as the first element of the syntax unless there is a "container" to exit the code flow an "return" to the code flow "out of" the container (method or function), so just have the function called as you first have it with out the "return" element, , as -
(function () {
ASKER
Hi Slick, the code does get encapsulated by other JS code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had a syntax error on my side. This works as designed.
So your code will look like this
Open in new window
See : https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/