0

Consider this code:

class ClassA {
    constructor(positon, obj) {
        this.position = positon
        this.obj = obj
    }
}
    
class ClassB {
    constructor() {
        this.position = [1, 2]
        this.obj = {
            pos: [1, 2, 3],
            rot: [4, 5, 6]
        }
        this.classAInstance = new ClassA(this.position , this.obj)
    }
}

class ClassC {
    constructor(position, obj) {
        this.obj = obj
        this.position = position
    }

    reassignObj(){
        this.obj = {
            pos: [4, 5, 6],
            rot: [7, 8, 9]
        }
    }

    resetObj() {
        this.obj.pos = [4, 5, 6],
        this.obj.rot = [7, 8, 9]
    }

    reassignPosition() {
        this.position = [3, 4]
    }

    resetPosition() {
        this.position[0] = 3
        this.position[1] = 4
    }
}
        
let fItem = new ClassB()
let other = new ClassC(fItem.position, fItem.obj)
other.resetObj()
other.resetPosition()
console.log('RE-SETTING')
console.log('FITEM', fItem.position, fItem.obj)
console.log('OTHER', other.position, other.obj)
fItem = new ClassB()
other = new ClassC(fItem.position, fItem.obj)
other.reassignObj()
other.reassignPosition()
console.log('RE-ASSINGNING')
console.log('FITEM', fItem.position, fItem.obj)
console.log('OTHER', other.position, other.obj)

When I reset the properties, changes are visible by other classes (reference copy is not changed). But when I reassign the array or the object, changes are not visible to the rest of the classes (reference copy is changed). As mentioned in the output below:

//RESETTING
FITEM [ 3, 4 ] { pos: [ 4, 5, 6 ], rot: [ 7, 8, 9 ] }
OTHER [ 3, 4 ] { pos: [ 4, 5, 6 ], rot: [ 7, 8, 9 ] } // changes visible to other
//REASSIGNING
FITEM [ 1, 2 ] { pos: [ 1, 2, 3 ], rot: [ 4, 5, 6 ] }
OTHER [ 3, 4 ] { pos: [ 4, 5, 6 ], rot: [ 7, 8, 9 ] } // changes not visible to other

Is there any way to make changes visible to other classes on reassignment to achieve kind o a pointer behaviour where changes are "visible" across many classes holding the reference?

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/q/518000) – VLAZ Apr 04 '23 at 18:20
  • There must be a better way to say what you mean than _"re-assing"_. Did you mean _reassign_? – Wyck Apr 04 '23 at 18:21
  • suggestions? as you can see in the snipet, by reassing I mean reassingning (sorry to use the same word) the object or array from the root instead of setting object's inner properties or array elements – rustyBucketBay Apr 04 '23 at 18:23
  • 1
    @rustyBucketBay: It's not clear to me specifically what you're trying to accomplish, perhaps the example is a bit too contrived for its complexity? Assigning to a variable and mutating an object referenced by a variable are certainly two very different things, and your code appears to illustrate that. But then what exactly are you asking? If you want a re-assignment to mutate whatever object was previously assigned to that variable then you'd need to re-think the approach, because re-assignment doesn't do that. – David Apr 04 '23 at 18:26
  • Assigning to a variable and mutating an object referenced by a variable are certainly two very different things -> for sure. I am aware. What I'd like to know if its possible to make a reassignment visible to other classes (same as if it was mutated) in Javascript – rustyBucketBay Apr 04 '23 at 18:29
  • You can read them again or even setup a getter and/or setter to do that for you. But that's not really the same as pointers. – VLAZ Apr 04 '23 at 18:31
  • Yes thanks. I know its not the same as pointers, but the proper way to achieve that behaviour in Javascript is what I am looking for. By that behaviour I mean make the root reassignment of objects and arrays visible to other classes. – rustyBucketBay Apr 04 '23 at 18:32

1 Answers1

0

Solution with getters and setters would be:

class ClassC {
    constructor(position, obj) {
        this._obj = obj
        this._position = position
    }

    reassignObj(){
        this.obj = {
            pos: [4, 5, 6],
            rot: [7, 8, 9]
        }
    }

    resetObj() {
        this.obj.pos = [4, 5, 6],
        this.obj.rot = [7, 8, 9]
    }

    reassignPosition() {
        this.position = [3, 4]
    }

    resetPosition() {
        this.position[0] = 3
        this.position[1] = 4
    }

    get obj() {
        return this._obj
    }

    set obj(value) {
        this._obj.pos = value.pos
        this._obj.rot = value.rot
    }

    get position() {
        return this._position
    }

    set position(value) {
        this._position[0] = value[0]
        this._position[1] = value[1]
    }
}
rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47