Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're really confused. Everything in JavaScript (except for primitive data types, <code>null</code> and <code>undefined</code>) is an object, and objects are stored in variables as reference. Read the following answer to know more about the differences between the two: <a href="https://stackoverflow.com/a/13268731/783743">https://stackoverflow.com/a/13268731/783743</a></p> <p>When you define two identical functions (in your case <code>x</code> and <code>a</code>) JavaScript sees them as separate functions. This is because in addition to the function body a function also maintains its own environment and state. Hence <code>x</code> and <code>a</code> are not the same function which is why <code>x === a</code> returns <code>false</code>.</p> <p>By setting <code>x = a</code> you're essentially copying the reference stored in <code>a</code> into <code>x</code>. Hence they now point to the same function (which is the function <code>a</code> was originally referring to). The function <code>x</code> was originally referring to is now lost and will eventually be garbage collected. Thus <code>x === a</code> now returns <code>true</code>.</p> <p>BTW you don't need to create a special <code>Contains</code> function to check whether an object is already inside an array. Just use <code>indexOf</code>:</p> <pre><code>var array = []; function x() {} array.indexOf(x); // returns -1 array.push(x); array.indexOf(x); // returns 0 </code></pre> <p>If the index is less than <code>0</code> the object is not in the array.</p> <p>If you want to check whether the function body of two functions is the same then use this function:</p> <pre><code>function sameFunctionBody(a, b) { return String(a) === String(b); } </code></pre> <p>Now <code>console.log(sameFunctionBody(x, a))</code> will return <code>true</code> as long as both the functions are <strong>exactly</strong> the same (including whitespace, parameters, function name, etc).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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