Javascript 局部变量生命周期
Javascript local variable lifetime
我在 C、C++ 方面很先进,但对 Javascript 还是个新手。我正在学习 three.js,但我无法理解这里发生的事情:
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 );
}
这是我想问的简化版。我已经阅读了很多文档和论坛,但我无法在任何地方找到它。
如果我在函数内部创建 "var light" 并用 "new" 赋值,它将正确创建对象。因为我在函数 "init" 的内部声明了它,所以它是局部变量。我了解到 javascript 中的对象是通过引用传递的。那么,如果我通过 "scene.add(light);" 将它添加到场景中,为什么它仍然有效?我传递了引用,然后对象本身应该被销毁,当我在 "init" 之外调用渲染函数时,它应该会失败,对吧?
我的问题是,如果 "new" 关键字以某种方式使局部变量在函数体结束时仍然存在,从而使对象在仍有引用的情况下保持活动状态?
我做对了吗?
虽然变量light
是在函数内部声明的,退出后就不存在了。变量引用的对象是 "added to the scene" 和 scene.add(light)
,并且可能会创建对该对象的第二个引用。一旦函数退出,第一个引用 light
超出范围并且引用被删除,留下来自 scene.add(light)
.
的引用
JavaScript 垃圾收集只会在没有对对象的引用时清理该对象。
new
除了创建对象的新实例外,没有做任何特别的事情。
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.
如果你的意思是 "reference" 像 C 和 C++ 参考,那么没有。如果您的意思是 Java,那么是的。有人称之为传递"copy-of-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?
对象不是在栈上创建的,只有指向它的值在栈中。该对象是在堆上创建的。由于您将对象传递给另一个方法,假设该方法将引用存储在某处,那么当您退出此函数时该对象将仍然存在。只有变量(持有 "pointer")会超出范围。
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?
当不再有对对象的引用(如 Java 中)时,该对象将被自动垃圾收集(如 C 中的 free
)。
我在 C、C++ 方面很先进,但对 Javascript 还是个新手。我正在学习 three.js,但我无法理解这里发生的事情:
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 );
}
这是我想问的简化版。我已经阅读了很多文档和论坛,但我无法在任何地方找到它。
如果我在函数内部创建 "var light" 并用 "new" 赋值,它将正确创建对象。因为我在函数 "init" 的内部声明了它,所以它是局部变量。我了解到 javascript 中的对象是通过引用传递的。那么,如果我通过 "scene.add(light);" 将它添加到场景中,为什么它仍然有效?我传递了引用,然后对象本身应该被销毁,当我在 "init" 之外调用渲染函数时,它应该会失败,对吧?
我的问题是,如果 "new" 关键字以某种方式使局部变量在函数体结束时仍然存在,从而使对象在仍有引用的情况下保持活动状态?
我做对了吗?
虽然变量light
是在函数内部声明的,退出后就不存在了。变量引用的对象是 "added to the scene" 和 scene.add(light)
,并且可能会创建对该对象的第二个引用。一旦函数退出,第一个引用 light
超出范围并且引用被删除,留下来自 scene.add(light)
.
JavaScript 垃圾收集只会在没有对对象的引用时清理该对象。
new
除了创建对象的新实例外,没有做任何特别的事情。
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.
如果你的意思是 "reference" 像 C 和 C++ 参考,那么没有。如果您的意思是 Java,那么是的。有人称之为传递"copy-of-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?
对象不是在栈上创建的,只有指向它的值在栈中。该对象是在堆上创建的。由于您将对象传递给另一个方法,假设该方法将引用存储在某处,那么当您退出此函数时该对象将仍然存在。只有变量(持有 "pointer")会超出范围。
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?
当不再有对对象的引用(如 Java 中)时,该对象将被自动垃圾收集(如 C 中的 free
)。