Javascript判斷對(duì)象是否相等實(shí)現(xiàn)代碼
更新時(shí)間:2013年03月18日 12:11:09 作者:
想判斷2個(gè)js對(duì)象,是不是所有完全相同在表單頁面應(yīng)用是很常見的,接下來分享一段判斷代碼,感興趣的你可以參考下哈,希望可以幫助到你
在做表單頁面的時(shí)候,想判斷2個(gè)js對(duì)象,是不是所有完全相同。
這里是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)該不陌生吧,下面為大家介紹下其精度問題,感興趣的朋友不要錯(cuò)過2013-11-11javascript中export?和export?default的區(qū)別
本文主要介紹了javascript中export?和export?default的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07JS基于對(duì)象的鏈表實(shí)現(xiàn)與使用方法示例
這篇文章主要介紹了JS基于對(duì)象的鏈表實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了鏈表的原理及javascript定義與使用鏈表的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01