I know that javascript always passes objects by reference. But It doesn't work if I try to return as a result of copying reference like this :
class TestClass {
constructor(){
this._name = "this is in the TestClass";
this._calclsObj = new CalCls(this);
}
calltest(subClass){
subClass = this._calclsObj; //here, it does nothing.
}
say(){
console.log(this._name);
}
}
class CalCls {
constructor(passedClass){
this._passedClass = passedClass;
this._passedClass._name = "This is in the Subclass";
}
say(){
console.log(this._passedClass._name);
}
}
let parent = new TestClass();
let child = {};
parent.calltest(child);
child //Object {} : nothing has assigned.
I tried subClass.__proto__ = this._calclsObj;
and Object.assign
. But That doesn't copy the reference of parent._calclsObj
. How can I assign parent._calclsObj
to child
itself by the reference?