[removed] how to visit the 'this' member of a construction function?

前端 未结 2 1385
陌清茗
陌清茗 2020-12-20 10:48

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()          


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 11:12

    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)

提交回复
热议问题