Beginners question - member variables


#1

I looked into the following code in the Hotspot example.
I dont understand where
for example

this.hitArea

etc come from.
They are not defined in code and I couldn’t find them in the api reference as welll
What am I missing ?

Hotspot.prototype.initialize = function() {
    // Create a hit area using a bounding sphere
    this.hitArea = new pc.BoundingSphere(this.entity.getPosition(), this.radius);
    // More information about pc.ray: http://developer.playcanvas.com/en/api/pc.Ray.html
    this.ray = new pc.Ray();
    
    this.defaultForwardDirection = this.entity.forward.clone();
    
    this.directionToCamera = new pc.Vec3();
    this.sprite = this.entity.children[0];
    
    // Register the mouse down and touch start event so we know when the user has clicked
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
    
    if (this.app.touch) {
        this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
    }
};

#2

this.hitArea it’s almost the same as var hitArea, in other words, it is a variable declaration.

In your case, there is the Hotspot class, which, using the prototype property, adds a new initialize method. Since the method is the same function, therefore, for the initialize method, the scope is limited to the anonymous function that is created. If inside the initialize method you create a variable using the var keyword, then this variable will be available only inside this method and it will be difficult to access it from other methods. If you declare a variable using the execution context of this, then this variable will be available in all methods of this class.


#3

Thanks a lot. Didn’t look as a declaration to me :slight_smile:
My JS seems a bit rusty