1.首先要先有兩個物件,如下舉例:
var obj1 = {
name:"xx",
sex:"man",
age:18
}
var obj2 = {
name:"xxs",
sex:"man",
age:18
}
2.判斷兩個物件是否相等:
function isObjectValueEqual (a, b) {
//取物件a和b的屬性名
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
//判斷屬性名的length是否一致
if (aProps.length != bProps.length) {
return false;
}
//迴圈取出屬性名,再判斷屬性值是否一致
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}
3.做出相應的操作:
var isChange = isObjectValueEqual(obj1,obj2);
if (isChange == true){
console.log('沒有修改,不需要提示是否儲存');
}else {
console.log('發生修改,提示是否儲存');
}