In the following code, the Boolean function is used to return a value. If a strict comparison is made of the result of two functions that return the same value, the result is true, but if two objects created by using new Boolean are compared, the result is false.
var salidas = [ Boolean(true), Boolean(true) === Boolean(true), new Boolean(true) === new Boolean(true), ] console.log(salidas.join('\n'));
Why is it that when you compare two objects that have been created using the same code, the result is false? How does the ECMAScript 2016 explain this?
I sense that the answer goes for the following
Boolean(true)returns a primitive Boolean data with value
new Bolean(true)returns an object that inherits the properties of object
- Every time it is called
new Bolean(true), there are objects that have the same structure inherited from the Boolean object, not to be confused with the Boolean primitive.
- By inherited structure I mean both objects will have the properties at least in name, however, there is something that makes the two objects different and therefore a strict comparison of these returns
What makes two objects that inherit properties of the same object different?
Apparently here the key concepts to differentiate are equality and identity
For equality , understand that two things are equal when they have the same values and when they are objects they have the same properties and they have the same values
For identity , understand that two things are identical, that is to say that they are not two things, but rather they are the same thing, both are one and only one thing.
Two instances of an object are not identical, although they were created almost at the same time, they are two independent things.
In the ECMAScript I did not find any mention of the above, but to some extent Equality comparisons and sameness
Note: The previous link points to the English version, since the Spanish version does not include references to the most recent versions of ECMAScript, although the English version does.