user3786755
user3786755

Reputation:

JavaScript Variable Updating

I noticed that in ThreeJs you:

    var a = new THREE.BoxGeometry();
    scene.add(a);
    a.position.z += 23.5;

You pass the objects cube into scene with all of its verticies etc. but then when you edit it's variables scene knows the new coordinates. How is this possible without passing a back into scene?

Upvotes: 0

Views: 32

Answers (1)

Anatoliy
Anatoliy

Reputation: 30073

It happened because you passed object a to scene.add method "by reference", so that all future changes of a are "known" by scene object.

To demonstrate this behaviour you can try the following code in your js console:

var a = {foo: 'bar'}, scene = {
    add: function(obj) {this.obj = obj},
    print: function() { console.log(this.obj.foo);}
});
scene.add(a);
scene.print(); // outputs bar
a.foo = 'baz';
scene.print(); // outputs baz

Upvotes: 1

Related Questions