Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>1) <code>fx</code> and <code>john.greet</code> are referring to the same function object, the assignment operation for objects, works <em>by reference</em>.</p> <p>For primitive values, like <code>String</code>, <code>Number</code>, <code>Boolean</code> <code>undefined</code> or <code>null</code>, a copy of the value will be made.</p> <p>2) The <code>this</code> value refers to the global object.</p> <p>The <code>this</code> value is not a property of the <a href="http://bclary.com/2004/11/07/#a-10.1.3" rel="noreferrer">Variable Object</a> and it has nothing to do with the <em>scope chain</em>, is a special reserved word, and it is determined implicitly when a <a href="http://bclary.com/2004/11/07/#a-11.2.3" rel="noreferrer">function is called</a> (you can also set it explicitly via <code>call</code> or <code>apply</code>).</p> <p>JavaScript internally handles a <a href="http://bclary.com/2004/11/07/#a-8.7" rel="noreferrer"><code>Reference type</code></a>, which consists of two components, the <em>base object</em> and the <em>property name</em>, when a function is invoked, the <code>this</code> value is determined implicitly by getting the <em>base object</em> (by the internal <a href="http://bclary.com/2004/11/07/#a-8.7.1" rel="noreferrer"><code>GetValue</code></a> operation).</p> <p>And finally, the last case where <code>this</code> is set implicitly is when you invoke a function with the <a href="http://bclary.com/2004/11/07/#a-11.2.2" rel="noreferrer"><code>new</code></a> operator, the <code>this</code> keyword will refer to a newly created object.</p> <p>So in brief, here is how <code>this</code> works <em>implicitly</em>:</p> <p>1- When a function is called as a method (the function is invoked as member of an object):</p> <pre><code>obj.method(); // 'this' inside method will refer to obj </code></pre> <p>2- A <em>normal</em> function call:</p> <pre><code>myFunction(); // 'this' inside the function will refer to the Global object // or (function () {})(); </code></pre> <p>3- When the <code>new</code> operator is used:</p> <pre><code>var obj = new MyObj(); // 'this' will refer to a newly created object. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload