利用展开操作符简化对象上属性添加的操作
发布日期:2021-05-13 01:36:15 浏览次数:18 分类:博客文章

本文共 1329 字,大约阅读时间需要 4 分钟。

������

���������������������������������������������������������������������������������������������

const params = {prop1:'1'}if(query){    params['prop2']= 2}

��������������������� spread���...������������������ if ���������������������

������������������������

const prop1 = 1,  prop2 = "2";const condition = false;console.log({ a: prop1, b: prop2, ...(condition ? { prop3: "3" } : {}) });// ������ { a: 1, b: '2' }

��������������� {} ������������ null���undefined ������������������������������������

���������������������������������������������������������

const prop1 = 1,  prop2 = "2";const condition = false;console.log({ a: prop1, b: prop2, ...condition && { prop3: "3" }});// ��������� { a: 1, b: '2' }

������ condition ��� true ��������������������������������������������������������� false������ false ������������������������

���������������������������������������������������������������������������������

const prop1 = 1,  prop2 = "2",  prop3=3;const condition = true;console.log({ prop1,prop2, ...condition && {prop3}});// ��������� {prop1: 1, prop2: "2", prop3: 3}

������

���������������������������������������������������������������

const items = [  'foo',  ... true ? ['bar'] : [],  ... false ? ['falsy'] : [],]// ������ ["foo", "bar"]

The text was updated successfully, but these errors were encountered:

上一篇:Java 获取Word中指定图片的坐标位置
下一篇:JavaScript 中正则匹配时结果不一致的问题

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月10日 11时25分45秒