Click here to Skip to main content
15,884,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Well query may seem rather Novice ,but still i would like some inputs at your end.
In JavaScript is it possible to access a Global Function from inside of a Local Function.

1> Is it possible...????
2> If yes, please enlighten... ;);)

Thanks in Advance :):)
Posted

1 solution

Of course it is possible. However, the question itself is not 100% correct.

Here is the thing: Javascript is an interpretive language, and functions are first-class citizens; they are also objects. It means that everything is created during execution, in the order of execution, everything, including the function. The whole idea of "local function" or "global function", as it is understood in the "traditional" imperative languages is somewhat blurred. Perhaps you mean the function object created during execution of some other function; such function is local and not visible from other functions, unless it is passed somewhere in some parameter (as function object, not to be mixed up with the result of calculation of function). Inside the scope of such "local" function, all object of outer scope are accessible, including function. If some function object is created at the moment of execution of some code of the inner function, such outer-scope function can be called.

There is nothing to enlighten here. Just call it.

The function scope is very well explained, for example, here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope[^].

—SA
 
Share this answer
 
Comments
MixBag 2-Mar-14 8:53am    
Sergey tx for the inputs....so what you are inferring is that if the function is passed as a parameter or arguement (which js treats as object) then it is possible right????
Sergey Alexandrovich Kryukov 2-Mar-14 12:16pm    
Calling a function is possible from any scope where its object is visible and initialized, even if not passed as a parameter. It can be written above the point of the call in the same of outer context, for example.

I mean that if a function object is passed as a parameter, it can be called elsewhere. This is not a real purpose of such parameters. The main purpose if the mechanism of abstraction. Say, you can pass not just different data to some function, but different data with different algorithm of processing this data. This is one of the basic features of functional programming.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900