Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器 软件资源

软件开发小程序制作系统集成与运维空间租用硬件开发视频监控技术咨询与支持——联系电话:0311-88999002/88999003

首页 / 脚本样式 / JavaScript / Javascript判断对象是否相等实现代码

在做表单页面的时候,想判断2个js对象,是不是所有完全相同。
这里是stackoverflow上的解决办法,在这里记录一下。
复制代码 代码如下:
Object.prototype.equals = function(x)
{
var p;
for(p in this) {
if(typeof(x[p])=="undefined") {return false;}
}
for(p in this) {
if (this[p]) {
switch(typeof(this[p])) {
case "object":
if (!this[p].equals(x[p])) { return false; } break;
case "function":
if (typeof(x[p])=="undefined" ||
(p != "equals" && this[p].toString() != x[p].toString()))
return false;
break;
default:
if (this[p] != x[p]) { return false; }
}
} else {
if (x[p])
return false;
}
}
for(p in x) {
if(typeof(this[p])=="undefined") {return false;}
}
return true;
}