0

It is hard to describe the situation without code. My modification made some answers irrelevant. I past the original code here and simplified version below:

function Entity(){
  var value = {};

  return {
    'value': value
  };
}

var e1 = Entity();
var e2 = Entity();
alert(e1.value === e2.value);

I thought it should return true. But in fact, it returns false. Is 'value' copied when it is returned from the function Entity?

update I think I know the reason now. each time Entity function is called the expression "var value={};" will generate a new Object. Thanks.

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
  • `{0, 1}` doesn't equal `{0, 1}`... same concept – jeremy Mar 30 '13 at 03:24
  • possible duplicate of [How do you determine equality for two JavaScript objects?](http://stackoverflow.com/questions/201183/how-do-you-determine-equality-for-two-javascript-objects) – jeremy Mar 30 '13 at 03:27
  • I know that {0,1} doesn't equal to {0,1}. My question is why e1.value and e2.value (in the simplified version) are not referencing to the same instance in the function. – Devs love ZenUML Mar 30 '13 at 04:26
  • Because the reference is recreated in each scope you create (i.e., a new "`value`" each time). Look [here](http://jsfiddle.net/k2hx6/): both `b` and `c` contain a reference to the original `a`. However, [here](http://jsfiddle.net/k2hx6/1/), both are referring to a different "`a`" – jeremy Mar 30 '13 at 04:34
  • Also - do you mind getting rid of the original version in your code above? It's just confusing to readers. – jeremy Mar 30 '13 at 04:38

3 Answers3

1

value is not copied when its returned, but a new Object is created whenever you run the Entity function.

You can observe the "new Object" creation with simple code like

console.log({} === {}) //should give false

var a = {};
console.log(a === a); //should give true

And you can check that things don't get copyed on return by assigning to more variables when running your function

var a,b,c;

function Entity(){
   var value = {};
   a = value;
   b = value;
   return value;
}

c = Entity();

console.log(a==b, a==c, b==c); //should all give true
Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 1
    It might also be worth mentioning that in general distinct Objects are never equal (in strict or abstract comparison), and that only numbers, strings and booleans can be compared. – Asad Saeeduddin Mar 30 '13 at 03:27
0

[elements...] syntax creates a new array. In javascript, === operator compares arrays by reference (not by contents), so the result is false

glebm
  • 20,282
  • 8
  • 51
  • 67
0

Your function is currently creating a new object each time it is getting called.

Keeping the same interface as in your example, to share the same array, you could do the following ->

Entity = (function () {
    //this variable is accessible only in the scope of this function
    var messageBoxes = ['hello'];

    //we return a function that return an object that will make the private messageBoxes variable accessible through it's messageBoxes property.
    return function () {
        return { messageBoxes: messageBoxes };
    };
})(); //note that this function will be self-executing


alert(Entity().messageBoxes === Entity().messageBoxes);

However, you would not be gaining anything here since you would be making the private messageBoxes variable publicly accessible directly.

plalx
  • 42,889
  • 6
  • 74
  • 90