以这种方式传递参数意味着什么? [英] What does passing arguments in this way mean?

查看:37
本文介绍了以这种方式传递参数意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习javascript,我看到了我不理解的这段代码:

I'm learning about javascript and I see this block of code that I don't understand:

exports.configure = ({
   expressapp = null,
   userdb = null,
   path = '/myroute'
} = {}) => { 
   // handle routes
};

我对传入的参数的结构及其内部发生的事情感到最困惑.在哪里可以找到有关此信息的更多信息,以便我可以阅读?当您传递这样的参数时,它甚至被称为什么?您为什么要这样做呢?

I'm most confused about the structure of the argument being passed in and what is going on inside there. Where can I find more information about that so I can read about it? What is it even called when you pass arguments like that? Why would you want to do it this way?

推荐答案

模式是一种解构分配,该分配将纯对象分配为默认参数,以避免在未将值传递给Type时避免 TypeError .功能.

The pattern is a destructuring assignment which assigns a plain object as the default parameter, to avoid TypeError if no value is passed to the function.

const exports = {};

exports.configure = ({
   expressapp = null,
   userdb = null,
   path = '/myroute'
}) => { 
   // handle routes
   console.log(expressapp)
};

try {
  exports.configure();
} catch(err) {
  console.error(err)
}

const exports = {};

exports.configure = ({
   expressapp = null,
   userdb = null,
   path = '/myroute'
} = {}) => { 
   // handle routes
   console.log(expressapp)
};

try {
  exports.configure();
} catch(err) {
  console.error(err)
}

这篇关于以这种方式传递参数意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆