In JavaScript, unlike C or C++, there is no direct reference mechanism. Values are passed by value and objects are passed by reference. However, you cannot directly modify the underlying reference like in C or C++:
// This does not work in JavaScript
let x = 27;
let y = &x;
*y = 28;
assert(x === 28);
One way to achieve similar functionality in JavaScript is shown below:
class B {
public foo: string;
}
class A {
public constructor(public b: B) {}
}
const b = new B();
b.foo = 'bar';
const a = new A(b);
b.foo = 'foobar';
assert(a.b.foo === 'foobar');
It's important to note that in this scenario, the reference is copied, not the entire object as in C++.