Note that there are some explanatory texts on larger screens.

plurals
  1. POReferences with variables in Javascript
    primarykey
    data
    text
    <p>I was pondering something earlier. I wanted to check if a function had already been put into an array. If so throw an exception. I did this little test with the console...</p> <p><img src="https://i.stack.imgur.com/57xcs.png" alt="enter image description here"></p> <p>So I guess I could say that the objects are always just references, and after setting <code>a</code> as <code>x</code> I could change <code>x</code> and <code>a</code> would be effected as well?</p> <p>Would this mean that the condition <code>x = a</code> no matter what, which is what I want.</p> <p>Using this to check if the function/object is already in the array I could just do this correct...</p> <p><img src="https://i.stack.imgur.com/uBV5u.png" alt="enter image description here"></p> <p>Is there a better way to do this?</p> <p>Would this also mean that if I pass a variable to a function and mutate it in that function it will be mutated outside of that function as well?</p> <p><strong>EDIT</strong></p> <p>I guess I am right about the mutation with this little test. But I don't get why its bar in the first log in the second example</p> <p><img src="https://i.stack.imgur.com/zK5sl.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/ZQ1fx.png" alt="enter image description here"></p> <p><strong>EDIT END</strong></p> <p>Example 1:</p> <pre><code>var x = function(){console.log("hello")}; var a = function(){console.log("hello")}; console.log(x == a); //comes out false //Set a as x a = x; console.log(x == a); //comes out true </code></pre> <p>Example 2:</p> <pre><code>Array.prototype.Contains = Array.prototype.Contains || function (obj) { return this.indexOf(obj) != -1; }; var x = function(){console.log("hello")}; var a = function(){console.log("hello")}; var z = a; console.log(x == a); //comes out false var l = []; l.push(x); //Set a as x a = x; l.push(a); console.log(x == a); //comes out true console.log(l.Contains(x)); //Should come out true console.log(l.Contains(a)); //Should come out true console.log(l.Contains(z)); //Should come out false </code></pre>
    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.
 

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