输出什么?
javascript
function compareMembers(person1, person2 = person) {
if (person1 !== person2) {
console.log("Not the same!")
} else {
console.log("They are the same!")
}
}
const person = { name: "Lydia" }
compareMembers(person)A. Not the same! B. They are the same! C. ReferenceError D. SyntaxError
答案: B
解析:
对象通过引用传递。 当我们检查对象的严格相等性(===)时,我们正在比较它们的引用。 我们将“person2”的默认值设置为“person”对象,并将“person”对象作为“person1”的值传递。 这意味着两个值都引用内存中的同一位置,因此它们是相等的。 运行“ else”语句中的代码块,并记录They are the same! 。