0

I have no idea how to describe my question .

(function(fn){
    var able=123;
    function tmp(){
        fn()
    };
    tmp();
})(function(){alert(able)});

This snippet throws a Reference Error :able is not defined' .

Would you please explain how javascript get variables to me ?

passion
  • 1,250
  • 9
  • 15
  • 1
    `able` is defined inside the IIFE only hence not accessible outside of it. – Tushar Aug 31 '16 at 07:16
  • 1
    That's because the IIFE has it's own scope -> [What is the (function() { } )() construct in JavaScript?](http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript) – adeneo Aug 31 '16 at 07:17
  • Is there a particular scope in `()` when function passed as a parameter ? – passion Sep 01 '16 at 05:01

4 Answers4

2

The scope of the "fn" function is not the same as the "parent" function, you should pass the "able" argument when you call the fn function, and then istantiate it in the fn function itself, like this:

(function(fn){
    var able=123;
    function tmp(){
        fn(able)
    };
    tmp();
})(function(able){alert(able)});
Roberto Lonardi
  • 569
  • 2
  • 10
  • You snippet goes well . But I don't think it can solve my question. I don't quite understand what they mentioned above , IIFE ! Quite confused. – passion Aug 31 '16 at 08:10
  • The only thing to understand is the variable declaration here, if you set the var declaration in a function, only that function and the functions inside it will have access to that variable, if we change the code like this: `var able=123; (function(fn){ function tmp(){ fn() }; tmp(); })(function(){alert(able)});` In this case "able" is in the upper scope, this allows the variable to be accessed in all functions that are at the same level or in a lower level. – Roberto Lonardi Sep 01 '16 at 09:05
  • I think maybe the scope of the variable in function is set when function decleared . – passion Sep 01 '16 at 09:51
  • In JavaScript, scope is the set of variables, objects, and functions you have access to. this scope is set when the paramter function is declared . I think so. – passion Sep 01 '16 at 09:55
  • Yes indeed is global scope, but if you wrap into another function: function(){ var able=123; (function(fn){ function tmp(){ fn() }; tmp(); })(function(){alert(able)}); } Able is not global, but is in an upper scope and is accessible from the lower scopes functions. – Roberto Lonardi Sep 01 '16 at 11:35
  • I think I have got the answer. **any function defined within another function has a local scope which is linked to the outer function**, the parameter function is not defined in the IIFE so `able` is not accessable .[link](https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/#scope-chain) – passion Sep 02 '16 at 00:17
  • That's exactly what happen :) – Roberto Lonardi Sep 02 '16 at 08:44
0

Functions create lexical closures at the time of their creation. This means that when your alert function is created, the able variable does not exist. Wrapping the execution of fn in a lexical closure that does know about able at a later moment does not affect the already created lexical closure of fn.

If you come up with a question that better explains what you are trying to do, we can propose how to properly use closures to express that idea.

Simon Meskens
  • 928
  • 1
  • 6
  • 12
0

java script support in function local and global scoping. in this case the "able" is local function scope you can't access outside the function.
If you use var the variable will be declared in the local scope. If u just declare the variable without a var, it will be declared in the global scope

0

Your code could be rewritten like this using named functions:

var func1 = function(fn) {
    var able=123;
    function tmp(){
        fn()
    };
    tmp();
}
var func2 = function() {
    alert(able)
}
func1(func2);

I believe this way it is clear that the variable 'able' is defined inside 'func1' (more precisely in its local scope) and you are trying to access it inside 'func2' which is outside the scope of 'func1' so it can not "see" into this scope.

More information about scoping in JavaScript can be found here: What is the scope of variables in JavaScript?

Community
  • 1
  • 1
Tomáš Cerha
  • 309
  • 1
  • 7