The constructor of an object that accepts parameters, but does not have "vars" inside the body of the constructor, where is the data?

Where is the data provided by the argument stored? Is var first created implicitly?

 function Student(first){ this.getFirst = function(){ return first; } } 

Tested with

var myStudent = new Student("ross");

console.log(myStudent); // Student {getFirst = function ()}

console.log(myStudent.getFirst()); // ross

console.log(first); // link error, not defined at first

console.log(myStudent.first); // undefined

for(var x in myStudent){ console.log(x); } for(var x in myStudent){ console.log(x); } // getFirst

My second question is: do I understand this correctly:

What happens with the "var" variables inside the JavaScript constructor?

"var" variables, "his" variables and "global" variables are inside the JavaScript constructor

... - this getFirst function creates a closure and saves the state of the constructor parameter value, if I use var first in the constructor body, is it good to consider it "encapsulation"? In addition, does any internal function store all parameter values ​​in a "fault state" or only in what is indicated by the internal function?

Thanks so much for your thoughts. This is my first question about S.O. but use the site almost daily as a link, so thanks for that. My programming knowledge is limited, so I apologize if I used crappy terms, I will be happy to clarify where this is necessary.

+4
source share
2 answers

The first parameter is stored locally in the function that is your constructor, so anywhere inside Student() it will be in scope.

More interestingly, the anonymous inner function that you assign this.getFirst closes this value. Because of this, the internal function maintains a reference to the first variable even after the constructor completes.

This also works for regular functions, and not just for constructors:

 function makeCounter() { var count = 1; return function() { return count++; }; } var counter = makeCounter(); console.log(counter()); // 1 console.log(counter()); // 2 console.log(counter()); // 3 

When used correctly, this approach can be used to achieve "private" variables in JavaScript, in the sense that the values ​​fixed in the closure are not accessible from the outside.

Because of this, it turns out that it does not matter if the closure covers all the variables in its scope or only those that it uses, since you cannot "get inside" these closed variables anyway. (Although in practice, only the values ​​used are usually fixed, so the runtime can garbage collect the rest because they are not needed.)

+4
source

To go a little deeper into what you are asking, it's not about creating var , but in your instance, the function also stores the arguments array.

Your functions can happily create short circuits on both.
When returning functions from your function, these returned functions (or objects with functions as methods) have access to any and all vars and arguments if you do not overwrite these functions.

The magic of closures.

+1
source

Source: https://habr.com/ru/post/1443131/


All Articles