Zeigerverhalten implementieren: Neuzuweisungen von Objekten oder Arrays für andere Klassen sichtbar machen
P粉899950720
P粉899950720 2023-09-11 08:57:15
0
1
377

Bedenken Sie den folgenden 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)

Wenn ich die Eigenschaft zurücksetze, können andere Klassen die Änderung sehen (die Referenzkopie wird nicht geändert). Aber wenn ich das Array oder Objekt neu zuweise, sehen andere Klassen die Änderung nicht (die Referenzkopie hat sich geändert). Wie in der Ausgabe unten gezeigt:

//重置
FITEM [ 3, 4 ] { pos: [ 4, 5, 6 ], rot: [ 7, 8, 9 ] }
OTHER [ 3, 4 ] { pos: [ 4, 5, 6 ], rot: [ 7, 8, 9 ] } // 更改对其他类可见
//重新分配
FITEM [ 1, 2 ] { pos: [ 1, 2, 3 ], rot: [ 4, 5, 6 ] }
OTHER [ 3, 4 ] { pos: [ 4, 5, 6 ], rot: [ 7, 8, 9 ] } // 更改对其他类不可见

Gibt es eine Möglichkeit, Änderungen bei der Neuzuweisung für andere Klassen sichtbar zu machen, um ein Zeigerverhalten zu implementieren, bei dem Änderungen in vielen Klassen, die Referenzen enthalten, „sichtbar“ sind?

P粉899950720
P粉899950720

Antworte allen(1)
P粉605233764

使用getter和setter的解决方案如下:

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]
    }
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!