方括号围绕函数定义中的参数 [英] Square brackets surrounding parameter in function definition

查看:160
本文介绍了方括号围绕函数定义中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ember CLI网站中看到了以下代码:

I came across the following code in the Ember CLI website:

export default Ember.Helper.helper(function([value]) {
  return value.toUpperCase();
});

让我感到困惑的是围绕值的方括号参数。我可以在函数调用中理解它,但是为什么在函数定义中?

What confuses me is the square brackets surrounding the value parameter. I can understand it in a function call, but why in function definition?

推荐答案

这对我来说非常令人惊讶,但它似乎是有效的javascript,根据ECMAScript 2017语言规范,函数声明中的形式参数可以是任何绑定元素,包括数组绑定。

This is all very surprising to me, but it appears to be valid javascript, according to the ECMAScript 2017 language specification, the formal parameter in a function declaration can any "binding element", including an array binding.

https://tc39.github.io/ecma262/#prod-BindingElement

此功能的实际行为似乎意味着函数的参数应该是一个数组,并且 value 将采用数组中第一个元素的值。

The actual behavior of this feature seems to mean that the argument to the function should be an array, and value will take on the value of the first element in the array.

这篇关于方括号围绕函数定义中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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