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 valuetrue
-
new Bolean(true)
returns an object that inherits the properties of objectBoolean
. - 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
false
.
What makes two objects that inherit properties of the same object different?
Note:
As I understand it, in colloquial terms we could say that the two objects are instances of the same class, however, I'm not sure that in strict terms this is correct since JavaScript is an object-oriented language, until version 6 class management was not implemented, but rather it is a typed language. In ECMAScript 2015 (version 6) class has been introduced, see 14 ECMAScript Language: Functions and Classes for the 2015 version and 14 ECMAScript Language: Functions and Classes for the 2016 version.
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.
It should be mentioned that "pure JavaScript" does not offer unique identifiers for objects, but some libraries or frameworks could do it.
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.
Related question