简体   繁体   中英

Javascript local variable lifetime

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?

Although the variable light is declared within the function and no longer exists after it exits. The object that the variable refers to is "added to the scene" with scene.add(light) and likely creates a second reference to the object. Once the function exits the first reference light goes out of scope and the reference is removed, leaving the one from scene.add(light) .

JavaScript garbage collection will only clean up a object once there are no references to it.

new does nothing special beyond creating a new instance of an object.

If I create "var light" inside function and assign it value with "new", it will create object right?

Yes.

Since I declared it INSIDE of the function "init" it is local variable. I've learned that objects in javascript are passed by reference.

If you mean "reference" like C and C++ references, then no. If you mean like in Java, then yes. Some people call it passing by "copy-of-reference", it's like passing a pointer to the object.

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?

The object is not created on the stack, only the value that points to it is in the stack. The object is created on the heap. Since you passed the object to another method, assuming that method stores the reference somewhere, the object will still exist when you exit this function. Only the variable (holding the "pointer") will go out of scope.

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?

The object will be automatically garbage-collected (like free in C) when there is no more references to it (like in Java).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM