In the following code, why can I access the variable x.b? Shouldn't it have a local scope?
CODE
function x() {
var a = 3;
}
x.b = 8;
console.log(x.a);
console.log(x.b);
OUTPUT
undefined
8
When you use var to declare a within x's constructor, a is mark as private, however when you do x.b you are essentially saying - add the property b to the object x.
Hence when you do x.b, technically speaking you are accessing object x's property b, which is 8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With