2

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.

taggedzi
  • 129
  • 4

5 Answers5

1

You can compare their constructors, as such

family.father.constructor == family.mother.constructor

MDC about constructors

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

JSFiddle

http://jsfiddle.net/8LvN5/

Robert
  • 21,110
  • 9
  • 55
  • 65
0

You could compare the prototype, based on prototypical inheritance.

EDIT: might be better to compare constructors, for browser compatibility...

family.father.__proto__ == family.mother.__proto__ //EDIT: doesn't work in IE
//so
family.father.constructor == family.mother.constructor

so, in general:

function isSameObject(obj1, obj2){ //is same prototype - actaully;
   return obj1.constructor == obj2.constructor
}

http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/

TastySpaceApple
  • 3,115
  • 1
  • 18
  • 27
0

Use constructors: http://jsfiddle.net/uME8m/

alert(family.father.constructor === family.mother.constructor); //true
alert(family.father.constructor === family.pet.constructor); //false
bjb568
  • 11,089
  • 11
  • 50
  • 71
0

You can compare using .constructor or .constructor.name.

family.father.constructor == family.mother.constructor
family.father.constructor == family.pet.constructor

More on objects you can see here.

If you want to construct a function, you can do:

function sourceObject(obj) {
  return obj.constructor; // you can return obj.constructor.name as well
}
Community
  • 1
  • 1
Beterraba
  • 6,515
  • 1
  • 26
  • 33
  • @Dude yes, but I let a link to a similar question, where he can read more information about javascripts object. – Beterraba Dec 29 '13 at 21:30
0

You can use Object.getPrototypeOf() (MDN Resource) to get the internal [[prototype]] property and then simply compare those two properties:

function isSameObject(obj1, obj2) {
    return Object.getPrototypeOf(obj1) === Object.getPrototypeOf(obj2);
}

This should work in every modern browser, starting with IE9.

The advantage compared to the constructor property, is that the constructor property can be easily accidentally overridden, for instance when implementing inheritance:

function A() {
}

function B() {
}

B.prototype = Object.create(A.prototype);

var b = new B();
console.log(b.constructor); //A

FIDDLE

basilikum
  • 10,378
  • 5
  • 45
  • 58