There is such a structure (which it would be desirable not to change dramatically):

var wrapper1 = function () { var wrapper2 = function () { return Math.PI; }; }; 

I tried using var caller = new wrapper1; call caller.wrapper2() , but returns undefined.
Tell me how to be, please. There is no way to understand the op in js.

    2 answers 2

    Replace var wrapper2 = ... with this.wrapper2 = ... When you write var inside a function, this variable (which refers to the function) is visible only inside the function. If you access this (in the case of javascript when creating a new instance of an object through new somefunction() this inside this constructor function itself points to the created object. Then through this.wrapper2 = ... you create a new field in the object and put in its own function.

    • Not so, then if there is such a structure, then everything will collapse. That won't work ... - VostokSisters
    • @VostokSisters you have a variable wrapper3 that gets the value returned by the wrapper2 function, not this.wrapper2. replace wrapper2 () with this.wrapper2 () in the body of the last anonymous function - selya
    • In principle, it works. . But with "use strict" - no ... Is this normal? - VostokSisters
    • Here is the proof . - VostokSisters
    • And where does this refer to? On the caller object or on the wrapper1 function wrapper1 ? - VostokSisters

    Of course, because wrapper2 is a private variable; it exists only during the execution of the function (or in the closure, which is not there).
    Use this :

     var wrapper1 = function () { this.wrapper2 = function () { return Math.PI; }; } var caller = new wrapper1; console.info(caller.wrapper2()); 

    • Not so, then if there is such a structure, then everything will collapse. That won't work ... - VostokSisters
    • In other words, wrapper2() should be visible in the wrapper1 osprey. - VostokSisters
    • @VostokSisters, and who's stopping you from calling this: return this.wrapper2(); ? - user207618
    • Yes, no one, but then it does not work with "use strict" ... - VostokSisters
    • If "use strict" is declared in wrapper1 . - VostokSisters