const createMember = ({ email, address = {} }) => {
const validEmail = /.+\@.+\..+/.test(email);
if (!validEmail) throw new Error("Valid email pls");
return {
email,
address: address ? address : null,
};
};
const member = createMember({ email: "my@email.com" });
console.log(member);
address
的默认值是一个空对象 {}
。当我们设置 member
变量为 createMember
函数返回的对象,我们没有为address参数传值,意味着address的值为默认的空对象 {}
。一个空对象是一个truthy值,意味着 address ? address : null
条件会返回 true
。address的值为空对象 {}
。