简体   繁体   中英

Use function of another scope to access variable of current execution context

I am developing a widget inside a framework so that I would need to log variable "a" using a pattern like the one below. How do I "call" this function log_a so it can access the variable declared in the execution context it is called from? ie Have the console output "a value", instead of the current error of not able to find variable "a".

(function(global) {

global.log_a = function() {
      console.log(a);
};

}(window));

var anotherFunction = function() {

    var a = 'a value';

    log_a();
};

anotherFunction();

edit:

I tried to make the example simple to make the question easier to follow but I now see it led to confusion. Update with more clarifications:

  • The first part of the code is trying to emulate my effort of moving large functions to a different file for cleaner code.

  • I am using a library where one input is a function and it will always pass one parameter to it, so I have no option of adding extra arguments (ie the answers suggesting to put "a" as an argument).

  • The trick of declaring "a" as global works and it is what I am currently using, but I thought this wasn't the best practice.

So my questions is (I am guessing the answer is "it is not possible") if there is some way to call log_a so it behaves like if the code was like this:

var anotherFunction = function() {

    var a = 'a value';

    var log_a = function() {
      console.log(a);
    };
};

anotherFunction();

Variable a exists only in the scope of function anotherFunction ;

(function(global) {

    global.log_a = function() {
        console.log(a);
    };

}(window));

var anotherFunction = function() {

    // var a exists only here, inside this function
    var a = 'a value';

    // log_a is another function, it has its own scope,
    // and it doesn't know about var a
    log_a();
};

anotherFunction();

You can either pass the variable a as an argument to log_a , like log_a(a) inside anotherFunction , or make the variable a available to both anotherFunction and log_a functions:

var a = 'a value';

(function(global) {

    global.log_a = function() {
        console.log(a);
    };

}(window));

var anotherFunction = function() {
    log_a();
};

anotherFunction();

You can use function arguments to pass the value of a to your log_a method. The issue that you are currently happening is that var a = 'a value' is unknown to the log_a as it is defined within another function. This is because a variable declared with var in a function scope cannot be accessed outside that function scope. Thus, you are better of doing something such as the following:

 (function(global) { global.log_a = function(a) { // retrieve 'a' as an argument in the log_a method console.log(a); // print the argument passed through }; }(window)); var anotherFunction = function() { var a = 'a value'; log_a(a); // pass through 'a' into the `log_a` method }; anotherFunction(); 

You can pass variable a value from that function so log_a() function can access that variable value.

  (function(global) { global.log_a = function(a){ console.log(a); }; }(window)); var anotherFunction = function() { var a = 'a value'; log_a(a); }; anotherFunction(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM