博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript函数参数
阅读量:2502 次
发布时间:2019-05-11

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

A function can accept one or more parameters.

一个函数可以接受一个或多个参数。

const dosomething = () => {  //do something}const dosomethingElse = foo => {  //do something}const dosomethingElseAgain = (foo, bar) => {  //do something}

Starting with , functions can have default values for the parameters:

从 ,函数可以具有参数的默认值:

const dosomething = (foo = 1, bar = 'hey') => {  //do something}

This allows you to call a function without filling all the parameters:

这使您可以在不填充所有参数的情况下调用函数:

dosomething(3)dosomething()

introduced trailing commas for parameters, a feature that helps reducing bugs due to missing commas when moving around parameters (e.g. moving the last in the middle):

引入了参数的尾部逗号,该功能有助于减少在参数周围移动(例如,在中间移动最后一个)时缺少逗号引起的错误:

const dosomething = (foo = 1, bar = 'hey',) => {  //do something}dosomething(2, 'ho!')

It is also okay to call your functions with a trailing comma after the last parameter:

它也还可以与最后一个参数后尾随逗号打电话给你的功能:

dosomething(2, 'ho!',)

You can wrap all your arguments in an array, and use the when calling the function:

您可以将所有参数包装在数组中,并在调用函数时使用 :

const dosomething = (foo = 1, bar = 'hey') => {  //do something}const args = [2, 'ho!']dosomething(...args)

With many parameters, remembering the order can be difficult. Using objects, destructuring allows to keep the parameter names:

使用许多参数,记住顺序可能很困难。 使用对象,解构可以保留参数名称:

const dosomething = ({ foo = 1, bar = 'hey' }) => {  //do something  console.log(foo) // 2  console.log(bar) // 'ho!'}const args = { foo: 2, bar: 'ho!' }dosomething(args)

Functions now support default parameters:

函数现在支持默认参数:

const foo = function(index = 0, testing = true) { /* ... */ }foo()

Default parameter values have been introduced in ES2015, and are widely implemented in modern browsers.

默认参数值已在ES2015中引入,并已在现代浏览器中广泛实现。

This is a doSomething function which accepts param1.

这是一个接受参数param1doSomething函数。

const doSomething = (param1) => {}

We can add a default value for param1 if the function is invoked without specifying a parameter:

如果在不指定参数的情况下调用函数,我们可以为param1添加默认值:

const doSomething = (param1 = 'test') => {}

This works for more parameters as well, of course:

当然,这也适用于更多参数:

const doSomething = (param1 = 'test', param2 = 'test2') => {}

What if you have an unique object with parameters values in it?

如果您有一个带有参数值的唯一对象怎么办?

Once upon a time, if we had to pass an object of options to a function, in order to have default values of those options if one of them was not defined, you had to add a little bit of code inside the function:

曾几何时,如果必须将选项对象传递给函数,如果未定义其中一个选项,则要具有这些选项的默认值,则必须在函数内部添加一些代码:

const colorize = (options) => {  if (!options) {    options = {}  }  const color = ('color' in options) ? options.color : 'yellow'  ...}

With object destructuring you can provide default values, which simplifies the code a lot:

使用对象分解,您可以提供默认值,从而大大简化了代码:

const colorize = ({ color = 'yellow' }) => {  ...}

If no object is passed when calling our colorize function, similarly we can assign an empty object by default:

如果在调用我们的colorize函数时没有传递任何对象,则类似地,我们可以默认分配一个空对象:

const spin = ({ color = 'yellow' } = {}) => {  ...}

翻译自:

转载地址:http://vxqgb.baihongyu.com/

你可能感兴趣的文章
有意思的cmd命令
查看>>
js正則表達式语法
查看>>
JVM-垃圾回收
查看>>
VS2013 添加已有文件夹
查看>>
python 计时程序运行时间
查看>>
【Shell脚本学习4】几种常见的Shell
查看>>
Git学习系列-Git基本概念
查看>>
c#多个程序集使用app.config 的解决办法
查看>>
模仿网站登录注册
查看>>
Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)
查看>>
Linux下获取本机IP地址的代码
查看>>
(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误
查看>>
flex布局
查看>>
python-----python的文件操作
查看>>
java Graphics2d消除锯齿,使字体平滑显示
查看>>
控件中添加的成员变量value和control的区别
查看>>
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>