Although I am adept in C and C++, Javascript is a new realm for me. Currently, I am delving into three.js but finding it challenging to grasp this particular concept:
var camera, scene, renderer, control;
init();
function init() {
scene = new THREE.Scene();
var light = new THREE.DirectionalLight( 0xffffff, 2 );
light.position.set( 1, 1, 1 );
scene.add( light );
}
This excerpt represents the core of my inquiry. Despite extensive reading across various sources, I have not encountered an explanation that clarifies my doubts.
My query pertains to the local variable "light" created within the function using the "new" keyword to instantiate an object. As per my understanding, objects in JavaScript are passed by reference. Therefore, when added to the scene with "scene.add(light);", why does it continue to function? Shouldn't the object get destroyed once the reference is passed, causing issues when invoking the render function outside of "init"?
Hence, my question revolves around whether the "new" keyword somehow enables the local variable to persist beyond the function's scope, ensuring the object remains active as long as there exist references pointing to it?
Could you confirm if my interpretation aligns with the actual behavior?