Actually, it's better to show the code, how can you make it work?

function Test(){ alert(myVar); } function Main(myFunct){ var myVar = 123; // Все объявленные переменные должны перебраться в область видимости переданной функции. В PHP, например, это делается с помощью extract() myFunct(); } Main(Test); // Передаем в Main некую функцию 

The fact is that now I have to do something like this:

 function Test(args){ alert(args.tmyVart) } function Main(){ Test({myVar: 123}) } Main(); 

This greatly reduces the readability of the code when args. are everywhere args.

  • why pass args if you can also pass myVar ? The code is probably a typo, because in the function Main - myVar , in the function Test - tmyVart - Grundy
  • Because the function can be passed xs how many arguments, we need an analogue extract in php - Fangog
  • how would extract be used in this case and how is it fundamentally different from the option with parameter passing? - Grundy

3 answers 3

In ECMA2015, the ability to use restructuring assignment in function parameters has been added.

It can be used as follows.

 function Test({ myVar: tmyVart }) { console.log('Test func','Parameter tmyVart: ', tmyVart); } function Test2({ myVar }) { console.log('Test2 func','Parameter myVar: ', myVar); } function Main() { Test({ myVar: 123 }); Test2({ myVar: 125 }); } Main(); 

    Write this:

     function Main(){ var myVar = 123; function Test(){ alert(myVar); } Test(); } 

    Your variable myVar is declared inside the function Main () and belongs to the Local context object of this function.

    When the interpreter enters the Test () function, the myVar variable in the context object does not perform this function. So, it must be somewhere. The interpreter goes to the scope property of the context object of the Test () function (it is also called Local ), which refers to the stack object (here objects of VO variables (variable objects of this context object and other context objects) are also Closure objects) and sees there is a Closure object and from it pulls out this myVar variable. This is a CLOSE. For this and come up with a circuit. you nest the body of function 2 in function 1 and you have access to variables and parameters from function 1 through a closure - a Closure object.

    That is, you can get a variable from another function, either as a parameter (as you did) or through a closure.

    • And if the Test function is passed to Main as an argument? (it was necessary to immediately mention it, hell, my slip) - Fangog
    • I'm confused. Correct your question so that it is clear that where it is being transferred - Dimon
    • Done, ferried the question - Fangog

    For example as follows:

     var sampleObject = { myVar: null, test: function() { alert(this.myVar); }, main: function() { this.myVar = 123; this.test(); } } sampleObject.main(); 
    • Just the essence of all this is to simplify the readability of the code, and do not use bla_bla.myVar, but simply write myVar - Fangog
    • 2
      @Fangog if accessing this for you reduces the readability of the code - I advise you to practice reading the code more! - Pavel Mayorov