Skip to content

输出什么?

Posted on:2021年7月3日 at 22:36
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的值为空对象 {}

原文转自:https://fe.ecool.fun/topic/a84229a5-810b-463d-9d53-d22fc59fc528