Im reading this article from Crockford: http://www.crockford.com/javascript/private.html
And in the section where he talks about Private, he says:
Private members are made by the constructor. Ordinary vars and parameters of the constructor becomes the private members.
Now if I do this in my script:
"use strict"
function car(brand) {
this.brand = brand;
var year = 2012;
var color = "Red";
}
var bmw = new car("BMW");
console.log(bmw.brand); // BMW -> VISIBLE ?!?
I can easily access property that was passed through constructor! Can someone explain this better, shouldn't these variables passed through constructor be private?
Thanks!
Everything you assign to the context (this inside function) is public available. Be aware that the context is the window object if you call the function without new