Solved

Having Returning  Anonymous Function Access Global Function

Posted on 2016-08-23
7
89 Views
Last Modified: 2016-09-22
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);
}

Open in new window

the following anonymous function calls global function and it works as expected:
// working anonymous function
 (function () {
 window._myGlobalFunct("value1");
 })()

Open in new window

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;
})();

Open in new window

0
Comment
Question by:894359
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 41768574
Use a function expression instead of function declaration.
So your code will look like this
_myGlobalFunct = function(str) {}

Open in new window

See : https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/
0
 
LVL 33

Expert Comment

by:Slick812
ID: 41768987
greetings 894359, , I looked at what you call an "anonymous function" as -

return (function() {
  var rvalue = document.title || "No Title";
 window._myGlobalFunct(rvalue);
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;

Open in new window


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(rvalue);

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

Open in new window

0
 

Author Comment

by:894359
ID: 41769279
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.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 33

Expert Comment

by:Slick812
ID: 41769697
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 () {
0
 

Author Comment

by:894359
ID: 41771174
Hi Slick, the code does get encapsulated by other JS code.

0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 41771821
OK, so it is part of a larger javascript Object structure, the Adobe DTM I guess, but I can not see or know the "Scope" of method variable inclusion, in an organization I do not know. If the "result" is not causing the problem, then my only guess is that the inclusion "Scope" does now include the -
      window._myGlobalFunct( )

but that does not seem posible to me? ? since you stated that the first code you had -
// working anonymous function
(function () {
window._myGlobalFunct("value1");
})()

worked?
So maybe someone else can help? ?
0
 

Author Comment

by:894359
ID: 41811722
I had a syntax error on my side. This works as designed.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now