前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >选择篇(112)-下面代码的输出是什么?

选择篇(112)-下面代码的输出是什么?

作者头像
齐丶先丶森
发布2022-12-05 13:58:20
1530
发布2022-12-05 13:58:20
举报
文章被收录于专栏:前端面试秘籍
代码语言: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!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-09-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端面试秘籍 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 答案: B
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档