对象笔试题
1.
const animals = {}; let dog = { emoji: '🐶' } let cat = { emoji: '🐈' } animals[dog] = { ...dog, name: "Mara" } animals[cat] = { ...cat, name: "Sara" } console.log(animals[dog])
答案:{emoji: "🐈", name: "Sara"}
因为属性名如果是非字符串的变量会通过tostring进行转换,dog和cat转换完都是一样的key ‘[object Object]’,
所以猫的会覆盖狗的
2.
const user = { email: "e@mail.com", password: "12345" } const updateUser = ({ email, password }) => { if (email) { Object.assign(user, { email }) } if (password) { user.password = password } return user } const updatedUser = updateUser({ email: "new@email.com" }) console.log(updatedUser === user)
答案: true 一直都在操作堆中的同一块空间