JavaScript:默认参数、扩展运算符、剩余参数和解构!

默认参数我们可以直接在参数列表中添加默认值function rolldie(numsides = 6) {return math.floor(math.random() * numsides) + 1;
}
登录后复制这里,需要注意秩序。默

javascript:默认参数、扩展运算符、剩余参数和解构!

默认参数

我们可以直接在参数列表中添加默认值

function rolldie(numsides = 6) {
  return math.floor(math.random() * numsides) + 1;
}

登录后复制

这里,需要注意秩序。默认参数只能出现在任何没有默认值的参数之后:

function greet(person, msg = 'hey there', punc = '!') {
  return `${msgs}, ${person}${punc}`;
}

登录后复制

传播

spread 语法允许在需要零个或多个参数(对于函数调用)或元素(对于数组文字)的地方扩展可迭代对象,例如数组,或者在需要零个或多个键的地方扩展对象表达式需要 – 值对(对于对象文字)。 -mdn

我们可以在数组上使用展开运算符:

console.log(math.max(1, 2, 3, 4, 5, 2)); // 5
const nums = [4, 3, 53, 3, 5, 2, 4, 920, 3, 5, 2];
console.log(math.max(...nums)); // 920

登录后复制

我们可以使用展开运算符来连接数组:

const cats = ['fluffy', 'zane', 'jim'];
const dogs = ['doggo', 'sir barks a lot'];
const allpets = [...cats, ...dogs, 'goldy'];
console.log(allpets); //['fluffy', 'zane', 'jim', 'doggo', 'sir barks a lot', 'goldy']

登录后复制

我们可以使用 spread 将属性从一个对象复制到另一个对象:

const feline = {
  legs: 4,
  family: 'felidae',
};
const canine = {
  family: 'canine',
  furry: true,
};

const dog = { ...canine, ispet: true };
console.log(dog); // {family: 'canine', furry: true, ispet: true}

// note, order matters - the last property takes precidence:
const catdog = { ...feline, ...canine };
console.log(catdog); // {legs: 4, family: 'canine', furry: true}

登录后复制

在数组和字符串上传播使用索引作为键值:

let newobj = { ...[2, 4, 6, 8] };
console.log(newobj); // {0: 2, 1: 4, 2: 6, 3: 8}

let anotherobj = { ...'hello' };
console.log(anotherobj); //{0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}

登录后复制

使用传播的一个更现实的例子是,如果我们想将数据添加到表单中:

const datafromform = {
  email: 'jim@jimelm.com',
  password: '1234',
  username: 'jimelm',
};

const person = { ...datafromform, id: 2134, isadmin: false };
console.log(person); // {email: 'jim@jimelm.com', password: '1234', username: 'jimelm', id: 2134, isadmin: false}

登录后复制

其余参数

休息与传播相反。它将一堆参数传递给函数并将它们组合成一个数组。一些例子包括:

function sum(...nums) {
  return nums.reduce((total, el) => total + el);
}

function raceresults(gold, silver, ...everyoneelse) {
  console.log(`gold metal goes to ${gold}`);
  console.log(`silver metal goes to ${silver}`);
  console.log(`and thanks to: ${everyoneelse}`);
}

登录后复制

解构

解构数组

这是解构数组的示例:

const scores = [999, 888, 777, 666, 555, 444];

const [gold, silver, bronze, ...otherscores] = scores;
console.log(gold); // 999
console.log(silver); // 888
console.log(bronze); // 777
console.log(otherscores); // [666, 555, 444]

登录后复制

解构对象

这里我们将解构一个对象:

const user = {
  email: 'marryelm@what.com',
  password: '134jsdf',
  firstname: 'marry',
  lastname: 'elm',
  born: 1927,
  died: 2091,
  city: 'hayward',
  state: 'ca',
};

const { email, state, city } = user;
console.log(email); // marryelm@what.com
console.log(state); // ca
console.log(city); // hayward

const { born: birthyear } = user;
console.log(birthyear); // 1927

登录后复制

我们可以为变量指定默认值,如下所示:

const user2 = {
  email: 'stacy@what.com',
  firstname: 'stacy',
  lastname: 'kent',
  born: 1984,
  city: 'boise',
  state: 'id',
};

const { city, state, died } = user2;
console.log(died); // undefined

const { city, state, died = 'n/a' } = user2;
console.log(died); // n/a

登录后复制

解构参数

我们还可以在函数参数内进行解构:

const user2 = {
  email: 'stacy@what.com',
  firstname: 'stacy',
  lastname: 'kent',
  born: 1984,
  city: 'boise',
  state: 'id',
};

function fullname({ firstname, lastname = '???' }) {
  return `${firstname} ${lastname}`;
}

登录后复制

我们还可以在回调函数中进行解构:

const movies = [
  {
    title: 'Indiana Jones',
    score: 77,
    year: 1994,
  },
  {
    title: 'Star Trek',
    score: 94,
    year: 1983,
  },
  {
    title: 'Deadpool',
    score: 79,
    year: 2001,
  },
];

let ratings = movies.map(({ title, score }) => {
  return `${title} is rated ${score}`;
});

console.log(ratings); // ['Indiana Jones is rated 77', 'Star Trek is rated 94', 'Deadpool is rated 79']

登录后复制

以上就是JavaScript:默认参数、扩展运算符、剩余参数和解构!的详细内容,更多请关注叮当号网其它相关文章!

文章来自互联网,只做分享使用。发布者:叮当,转转请注明出处:https://www.dingdanghao.com/article/705252.html

(0)
上一篇 2024-08-10 17:41
下一篇 2024-08-10 17:41

相关推荐

联系我们

在线咨询: QQ交谈

邮件:442814395@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信公众号