In Javascript (no jquery or other frameworks please) I want to test if 2 (or more) objects are the same type of object.
Here are some sample objects:
function Person (firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
function Animal (name, type, age) {
this.name = name;
this.type = type;
this.age = age;
}
var family = {};
family.father = new Person ('John', 'Doyle', 33);
family.mother = new Person ('Susan', 'Doyle', 32);
family.pet = new Animal ('Jodie', 'Cat', 2);
Given the above objects, I want a generic way to test if family.father, family.mother, and family.pet are from the same "type" of object. Can I test for the difference between Person and Animal in a generic way. Something like:
if ( sourceObject(family.father) === sourceOjbect(family.mother) ) {
alert('father and mother are the same type');
} else {
alert('father and mother are not from the same object... we could have problems.');
}
if (sourceObject(family.pet) !== sourceObject(family.father)) {
alert('the pet is a different type than the father');
} else {
alert('Perhaps it should be a child instead of a pet?');
}
or perhaps something like this:
if (isSameObject(family.pet, family.father)) {
alert('Perhaps it should be a child instead of a pet?');
} else {
alert('Father and pet are different.');
}
There is no "sourceObject" function, but I'm trying to find out if there IS something that does this or someone has found a quick way to do that comparison.
Note I am not looking to compare the data in the objects, I am looking to compare the type of Objects involved in the comparison. I need to do this without knowing the exact make up of the components involved. For example, in the above code I could test for a "type" property, but that requires foreknowledge of the objects. I am trying to write a generic function that will not necessarily know anything about the objects passed to it other than the objects them selves.