Skip to content Skip to sidebar Skip to footer

Javascript: How To Visit The 'this' Member Of A Construction Function?

In console, I created a construction function 'Car' like below, and new-ed a object named 'mycar', and it happend like this: > var Car = function() { ... this.make = 'Ford'; .

Solution 1:

The this keyword in the function refers to the object being created by the new keyword.

If you want to create a "static" property, you would have to set it directly on the function name like this:

function Car() {
}

Car.make = "Ford";

Basically you can't have "both". The property is either static (on the function itself) or instance (on the object created by new)

Post a Comment for "Javascript: How To Visit The 'this' Member Of A Construction Function?"