title: Typescript: Pass by value or by reference
body: <p>I have this file:</p>
  
  <p>object.ts:</p>
  
  <pre><code>export const myObject {
      prop1: "prop1",
      prop2: "prop2",
      ...
      ...
  }
  </code></pre>
  
  <p>and I have this class</p>
  
  <p>my-class.ts</p>
  
  <pre><code>export class MyClass {
      private obj: any;
  
      constructor(obj: any) {
          this.obj = obj
      }
  }
  </code></pre>
  
  <p>and I have this file:</p>
  
  <p>main.ts</p>
  
  <pre><code>import { myObject } from "object";
  import { MyClass } from "my-class";
  
  
  let class1 = new MyClass(myObject);
  let class2 = new MyClass(myObject);
  let class3 = new MyClass(myObject);
  </code></pre>
  
  <p>will creating instances of the class <code>MyClass</code> this way pass the object <code>myObject</code> by value or reference. In another way, will the <code>myObject</code> object be copied three times; I'm concerned about the memory.</p>
  
  <p><strong>UDPATE</strong></p>
  
  <p>Consider this other example:</p>
  
  <p>my-class.ts</p>
  
  <pre><code>export class MyClass {
      private obj: any;
  
      constructor(obj: any) {
          this.obj = obj
      }
      getValue(str: string) {
          return obj[str];
      }
  }
  </code></pre>
  
  <p>function1.ts</p>
  
  <pre><code>import { myObject } from "object";
  
  let myClass = new MyClass(myObject)
  
  export function fn1(str: string) {
      return myClass.getValue(str);
  }
  </code></pre>
  
  <p>function2.ts</p>
  
  <pre><code>import { myObject } from "object";
  
  let myClass = new MyClass(myObject)
  
  export function fn2(str: string) {
      return myClass.getValue(str);
  }
  </code></pre>
  
  <p>main.ts</p>
  
  <pre><code>import { fn1 } from "function1";
  import { fn2 } from "function2";
  
  console.log(fn1(str));
  console.log(fn2(str));
  </code></pre>
  
  <p>Will the second example create two copies of <code>myObject</code>?</p>