When comparing two equal Booleans it returns true but not when comparing two equal objects no Why?

13

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 true
  • new Bolean(true) returns an object that inherits the properties of object Boolean .
  • 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
asked by Rubén 11.07.2017 в 16:46
source

3 answers

8

Update

I can see where your question is going. In JavaScript objects are stored by reference, therefore, what you should be comparing are the memory locations and these are different for each instance.

Well, as I see it, the operator new creates instances of an object. In my opinion it would be a mistake to consider that two instances are exactly the same. The same goes for other objects:

console.log(new Array() === new Array());
console.log(new Object() === new Object());

The correct way to compare the new instances is with its value:

var salidas = [ 
  Boolean(true),
  Boolean(true) === Boolean(true),
  (new Boolean(true)).valueOf() === (new Boolean(true)).valueOf(),
]

console.log(salidas.join('\n'));
    
answered by 11.07.2017 в 17:31
3

The error lies in the fact that Boolean(true) returns a value booleano a primitive datum, however making new Boolean(true) the return value is a objeto that wraps the primitive data true

typeof Boolean(true) // boolean
typeof new Boolean(true) // object

Comparing two objects is usually done by reference, that is, doing

objA === objB 

Returns true yes and only yes objB is a alias of objA

So if you want to compare objects based on their properties, you should implement it, as in the following snippet

Object.prototype.igualA = function(obj)
{
	var _this = this // extiende el alcance
	var mismo_constructor = _this.constructor === obj.constructor

	var mismas_propiedades = function()
	{
		var propiedades = Object.getOwnPropertyNames(_this)
		for(var i in propiedades)
		{
			var propiedad = propiedades[i]
			if(_this[propiedad] !== obj[propiedad]){ return false }	
		}	
		return true
	}()

	return mismo_constructor && mismas_propiedades
}

var x = new Boolean(true)
var y = new Boolean(true)
console.log(x.igualA(y))
    
answered by 11.07.2017 в 18:26
1

In Javascript, objects are created by reference. That means that when obj1 == obj2 is made, what is compared is the reference, that is, it will return true only if it is the same object, that is, obj1 and obj2 point to the same address in memory. / p>

To verify the equality of objects of the same class, a method equals(other) must be defined. For example

function Person(name){
    this.name = name;

    var self = this;
    this.equals = function(other){
        return (other instanceof Person) && self.name == other.name;
    }
}

var p1 = new Person("Juan");
var p2 = new Person("Pedro");
var p3 = new Person("Juan");

alert(p1.equals(p2)); // Devuleve false
alert(p1.equals("Juan")); // Devuelve false, "Juan" no es una instancia de la clase Person, sino un string
alert(p1.equals(p3)); // Devuelve true

Print:

false
false
true
    
answered by 25.07.2017 в 22:05