As you know, in javascript functions are objects, my problem is with the following code:
function Car() {
this.color = "black";
}
console.log(Car.name);
console.log(Car.color);
Output: Car undefined
Since I could access name property of Car object why can't I access color property of Car object in the same way.
Another example:
console.log("Hello".charAt.name);
console.log("Hello".charAt.length);
Output : charAt 1
Here charAt is a method of String object but I used its name as a reference to access name and length properties and not only these properties but also some methods such as : hasOwnProperty and isPrototypeOf
My question is what exactly those properties and methods?
color
is set at the construction time to the object of the typeCar
and is not a property of the type itself. (carObj = new Car(); console.log(carObj.color);
)this.color = "black";
is never ever execute. If you do aconsole.dir(Car)
you can see which properties function have.