2

Is there any way to capture the whole function closure and then refer to it in other places of code? I want to do something like that:

var captured_closure;
function anything() {
    var x = 10;
    var y = "k";
    captured_closure = current_function_closure;
}

anything();
console.log(captured_closure.x); //Should be 10
console.log(captured_closure.y); //Should be k

May goal is to export all variables processed by a particular function (I may do it one-by-one but it would be more convenient to have them all) and then to manipulate them in developer tools for debugging purposes. Thank you.

[edit] Sorry, I missed vars inside the function previously. It's corrected now.

Liberat0r
  • 1,852
  • 2
  • 16
  • 21
  • Possible duplicate of [Get list of closure variables in Javascript](https://stackoverflow.com/questions/35223591/get-list-of-closure-variables-in-javascript) – Oleksandr Martyniuk Aug 29 '18 at 13:19

1 Answers1

-1

You can use bind to send some variables to your function anything

var dataObj = {
  x: 10,
  y: 'k'
};

function anything() {
  console.log(this.x);
  console.log(this.y);
}


var runAnything = anything.bind(dataObj);
runAnything();
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
аlex
  • 5,426
  • 1
  • 29
  • 38