I am advanced in C, C++ but new to Javascript. I am learning three.js and I have trouble understanding what is happening in here:
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 is simplified version of what I want to ask. I've read a lot of docs and forums, but I could not find this anywhere.
If I create "var light" inside function and assign it value with "new", it will create object right. Since I declared it INSIDE of the function "init" it is local variable. I've learned that objects in javascript are passed by reference. So if I add it to the scene by "scene.add(light);", why it still works? I passed the reference, then the object itself should be destroyed and when I'll call render function outside of "init" it should fail right?
My question is, if the "new" keyword somehow makes the local variable persist even when the function's body ends, keeping the object alive while there is still something holding reference to it?
Did I get this right?
std::shared_ptrin C++) - UnholySheepnewisn't what matters.scene.add()likely keeps a reference to thelight. - zero298