我正在尝试比较两个对象的键,属性值无关紧要。
var obj1 = {
foo: {
abc: "foo.abc",
},
bar: {
aaa: {
bbb: "bar.aaa.bbb" // <-- difference
}
}
};
var obj2 = {
foo: {
abc: "foo.abc",
},
bar: {
aaa: {
ccc: "bar.aaa.ccc" // <-- difference
}
}
};
// function should return true if properties are identical, false otherwise
function compareObjProps(obj1, obj2) {
for(var prop in obj1) {
// when comparing bar.aaa.bbb and bar.aaa.ccc
// this does get logged, but the function doesn't return false
if(!obj2.hasOwnProperty(prop)) {
console.log("mismatch found");
return false;
}
if(typeof(obj1[prop]) === "object") {
compareObjProps(obj1[prop], obj2[prop]);
}
}
// this always returns
return true;
}
似乎return false
不是从顶层函数返回,而是从递归函数返回。
那么,当整个匹配函数执行完毕后,如何返回 false 呢?