Javascript判斷對象是否相等實現(xiàn)代碼
更新時間:2013年03月18日 12:11:09 作者:
想判斷2個js對象,是不是所有完全相同在表單頁面應(yīng)用是很常見的,接下來分享一段判斷代碼,感興趣的你可以參考下哈,希望可以幫助到你
在做表單頁面的時候,想判斷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;
}
這里是stackoverflow上的解決辦法,在這里記錄一下。
復(fù)制代碼 代碼如下:
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;
}
相關(guān)文章
javascript的parseFloat()方法精度問題探討
javascript中的parseFloat()方法,大家應(yīng)該不陌生吧,下面為大家介紹下其精度問題,感興趣的朋友不要錯過2013-11-11javascript中export?和export?default的區(qū)別
本文主要介紹了javascript中export?和export?default的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07