如何测试两个javascript对象是否相似?

如何测试两个javascript对象是否相似?,javascript,object,Javascript,Object,在Javascript中(请不要使用jquery或其他框架),我想测试2个(或更多)对象是否是相同类型的对象 以下是一些示例对象: function Person (firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } function Animal (name, type, age) { this.name = nam

在Javascript中(请不要使用jquery或其他框架),我想测试2个(或更多)对象是否是相同类型的对象

以下是一些示例对象:

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);
鉴于上述对象,我需要一种通用的方法来测试family.father、family.mother和family.pet是否来自同一“类型”的对象。我可以用一种通用的方法来测试人和动物之间的区别吗。比如:

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?');
}
或者像这样:

if (isSameObject(family.pet, family.father)) {
    alert('Perhaps it should be a child instead of a pet?');
} else {
    alert('Father and pet are different.');
}
这里没有“sourceObject”函数,但我试图找出是否有什么东西可以做到这一点,或者有人找到了一种快速的方法来进行比较


注意:我不是要比较对象中的数据,而是要比较比较中涉及的对象类型。我需要在不知道所涉及组件的确切组成的情况下执行此操作。例如,在上面的代码中,我可以测试“type”属性,但这需要预先了解对象。我正在尝试编写一个泛型函数,它不一定知道传递给它的对象的任何信息,除了它们自己的对象。

您可以比较它们的构造函数

family.father.constructor == family.mother.constructor
关于构造函数的MDC

JSFIDLE


您可以基于原型继承比较原型

编辑:为了浏览器兼容性,比较构造函数可能更好

family.father.__proto__ == family.mother.__proto__ //EDIT: doesn't work in IE
//so
family.father.constructor == family.mother.constructor
因此,一般来说:

function isSameObject(obj1, obj2){ //is same prototype - actaully;
   return obj1.constructor == obj2.constructor
}
使用构造函数:


您可以使用
.constructor
.constructor.name
进行比较

family.father.constructor == family.mother.constructor
family.father.constructor == family.pet.constructor
更多关于您可以看到的对象的信息

如果要构造函数,可以执行以下操作:

function sourceObject(obj) {
  return obj.constructor; // you can return obj.constructor.name as well
}

您可以使用
Object.getPrototypeOf()
()获取内部
[[prototype]]]
属性,然后简单地比较这两个属性:

function isSameObject(obj1, obj2) {
    return Object.getPrototypeOf(obj1) === Object.getPrototypeOf(obj2);
}
这应该适用于从IE9开始的所有现代浏览器

构造函数
属性相比,它的优点是
构造函数
属性很容易被意外覆盖,例如在实现继承时:

function A() {
}

function B() {
}

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

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

这个问题有帮助吗?这可能有助于
typeof
操作员工作?@mathguy54
typeof
对象返回给所有cases@osi:否,这也会比较对象中的数据。因此,family.father和family.mother的比较是错误的。family.father.prototype==family.pet.prototype表示同意。以这种方式获取原型是行不通的。已编辑。@Dude是的,但我提供了一个指向类似问题的链接,在这里他可以阅读有关javascripts对象的更多信息。
function A() {
}

function B() {
}

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

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