【发布时间】:2020-02-25 20:26:33
【问题描述】:
在 python 中,我们可以在函数调用中省略带有默认值的参数。例如
def post(url, params={}, body={}):
print()
print(url)
print(params)
print(body)
post("http://localhost/users")
post("http://localhost/users", {"firstname": "John", "lastname": "Doe"}) # Skipped last argument (possible in JS as well)
post("http://localhost/users", params={"firstname": "John", "lastname": "Doe"}) # same as above
post("http://localhost", body={"email": "user@email.com", "password": "secret"}) # Skipped 2nd argument by passing last one with name (i.e. "body")
我可以在 JS 中实现这一点吗?就像省略第二个参数,只传递最后一个。 (如在最后一种情况下)。其他情况在 JS 中是可能的,但我找不到实现最后一个的方法
【问题讨论】:
-
[Skip arguments in a JavaScript function ](stackoverflow.com/questions/32518615/…)的可能重复项
标签: javascript python function parameters optional-parameters