var Car = function () {
this.wheels = 4;
};
var car = new Car();
console.log(car.wheels);
I'm new to javascript and I'm trying to understand the concept of scope
.
Here Car
is assigned with a function which we call a constructor. And var car = new Car();
creates a instance of a so called Car
class.
I think var car = new Car()
is actually creating a function and we use it to mock an instance variable in conventional OOP languages.
If that is right, var car
should be a function and has its own 'scope' and we can not access its variables outside.
So why the last expression can get the value of car.wheels
and print the value?
It is called from the global scope!?