Reputation: 267059
If I have this code:
var foo1 = new Foo();
var foo2 = new Foo();
and I want to do this:
function updateFoo1 (foo)
{
if (foo === foo1) //If foo is foo1
{
//code for processing
return true;
}
else //Foo must be foo2 or another instance of foo not foo1
return false;
}
Will this work as expected to make sure whether the function argument foo
is the same as foo1
and not foo2
or any other instance of the foo
class? If not, what should I do to get the result I want?
Upvotes: 1
Views: 98
Reputation: 146191
function Foo(){}
var foo1 = new Foo();
var foo2 = new Foo();
console.log(foo1===foo2); // always false
Object comparison is being done by object's reference so foo1
returns a different reference and foo2
returns another reference. In your case
function updateFoo1(foo)
{
if (foo === foo1)
{
//code for processing
return true;
}
else return false;
}
updateFoo1(foo1) // first if condition will work and will return true
updateFoo1(foo2) // else condition will work and will return false
Upvotes: 2