I have written a prototype method that I would like to extend the Array object with:
Array.prototype.indicesOf = function(val) {
var res = [];
for (var a=0;a<this.length;a++) {
if (this[a] === val) {
res.push(a);
}
}
return res;
}
When I go to test it, I'm expecting to get strict equality, but I'm not.
Here is that test:
var res1 = document.getElementById('result_1');
var res2 = document.getElementById('result_2');
Array.prototype.indicesOf = function(val) {
var res = [];
for (var a=0;a<this.length;a++) {
if (this[a] === val) {
res.push(a);
}
}
return res;
}
var a = ["a", "c", "e", "a", "b", "d", "a", "f"];
res1.innerHTML = a.indicesOf("a").toString();
var b = [{name : "test1"}, {name : "test2"}];
res2.innerHTML = b.indicesOf({name : "test1"});
<div id="result_1"></div>
<div id="result_2"></div>
I'm not sure what I am doing wrong, and I was hoping someone could help with an answer that explains why my case for var a
is working as expected, but my case for var b
is not being considered equal.
Can someone help me gain some comprehension as to what is happening when comparing equality among (not between) different data types?
This has already been marked as a duplicate, which I highly disagree with, but let me explain why, and try to do this in a logical way. I'm not trying to offend anyone (it's simply not my intent), and I truly would like to know the answer.
The reasoning: The links provided to justify the marked as duplicate, themselves link to other links, which link to more links that don't really help me understand the problem of why it doesn't seem possible to compare different data types using the equality operators ===
.
They tell you that it will equal false when comparing either an Object to an Object or an Array to an Array, but don't really explain why.
When I have a problem:
Let's call this one 'Problem A' 1 + 9
And
Let's call this one 'Problem B' 8 + 2
I can check to see if these two separate problems are equal, and the answer is yes, because they equate to the same answer.
When using the same basic logic looking at an Object which has the same properties however, in my mind they should equate to the sum of their parts (properties in this case) logically.
Implicating this:
var additionProblem = function(a1, a2) {
this.addend1 = a1;
this.addend2 = a2;
this.answer = this.addend1 + this.addend2;
}
var problem1 = new additionProblem(1, 9);
var problem2 = new additionProblem(8, 2);
//When compared via ===
//(problem1 === problem2) returns false (understandable)
//(problem1.answer === problem2.answer) returns true (understandable)
//BUT
//(9+1 === 8+2) returns true (also understandable)
//({name : "problem1"} === {name : "problem1"}) returns false (not understandable using this logic)
//
If I can compare one PROBLEM to another that is the sum of its (different) parts and have it return true
, and yet at the same time compare two objects/arrays that have even more exacting equality than the two obviously different PROBLEMS, then I would really like to know how to properly use the ===
&/OR ==
for my purposes please.
The reasons given in the so called answers and comments of the linked justifications are seriously lacking what others have settled for as answers, and do not even come close to asking what I am asking for.
The best I can glean from these answers are simply solutions to approach the underlying problem of Javascript having no built-in way of handling Object-to-Object
equality comparison, NOT memory pointer comparison.
I would like to know, why by design of the language specification there is not a built-in operator to thoroughly check the contents (Key/Value pairs) of one instance of an object, and compare it to the contents of another instance of an object.
Can anyone shed light on this?
Is there any plans for this type of functionality to be incorporated into the language in the future, or will this be like Promises, where everyone builds a different implementation of it into a library, and then eventually have it become part of the language standard, once it gets hashed out in the open source community?