Question about a demo project

hello
inspecting this project

I came across the script orbitCamera.js
https://playcanvas.com/editor/code/629759?tabs=21687458,21687448,21687457

and the Object.defineProperty(OrbitCamera.prototype, "yaw", {...}
lines 57- 78.
I’m struggling to understand where did this._yaw came from.
the definition is for “yaw”, not “_yaw”
same goes for “_targetYaw”

That line is defining the property ‘yaw’ for the orbit camera and creating getter and setter functions for that property.

If you look further down to the initialisation function, you can see this._yaw and this._targetYaw being defined.

Line 181 and 187.

hi

so defining this._yaw creates the variable? (line 181)
what’s the difference between that and var _yaw? scope?

JS is an odd language if you have come from a C# or C++ background. It’s weakly typed and you can add properties to objects at runtime.

It assigns a value to the property _yaw to the this object. The this object in this case refers to the class instance so it can be accessed in any of the functions in the class.

If initialize function is never called, then there would be no value assigned to _yaw and therefore it would be undefined if you tried to access that property.

var someVariable is function scoped if declared in a function.

thus
since initilize is called on the 1st frame,

i can define variable and properties as i please by doing
this.someNewProperty = …

and it will than be accessible from anywhere in the script, correct.

will it then be accessible from other scripts? or is the scope of existence is only script wide?

Accessible from any function for that class, yes. Not necessarily anywhere in the script file. Consider the scoping of the this reference.

JS has no concept of private yet so as long as you have a reference to the class instance, you can access any property and any function that is associated with that instance.