Where is the data supplied by the argument being stored? Is a var first
being 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);
// reference error, first not defined
console.log(myStudent.first);
// undefined
for(var x in myStudent){
console.log(x);
}
// getFirst
My second question is if I understand these correctly:
What happens with "var" variables inside a JavaScript Constructor?
“var” variables, "this" variables and "global" variables - inside a JavaScript Constructor
...is the getFirst
function creates a closure and saves the state of the constructor's parameter value, if I use a var first
in the constructor body is it okay to think of that as 'encapsulation'? Additionally, any inner function saves all the parameter values in a "closure state" or just the one's referenced by the inner function?
Thank you very much for your thoughts. This is my first question on S.O. but use the site almost daily as a reference, so thank you for that. My programming knowledge is limited so pardon if I've used crappy terms, happy to clarify where needed.