title: Access calling object from Javascript method
body: <p>In JavaScript, for an anonymous function defined inside the object, the <code>this</code> keyword refers to the object itself. However, when the function is defined outside the object scope, <code>this</code> refers to the window object, example:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function foo() {
    console.log(this)
  }
  
  let A = {
    att: foo(),
    att2: "bla"
  }
  
  A.foo();</code></pre>
  </div>
  </div>
  </p>
  
  <p>[EDIT]: I made a mistake in the original code, I meant to call the function as A.att() (and I am obliged to keep att: foo(). My code is basically the following:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function foo() {
    console.log(this)
  }
  
  let A = {
    att: foo(),
    att2: "bla"
  }
  
  A.att();</code></pre>
  </div>
  </div>
  </p>
  
  <p>The output here is object Window, and not the object that I want to access, which is A. And I can't change A, so how can I access A from foo()?</p>