是否可以在缺省参数设置默认参数值 [英] Is It Possible To Set Default Parameter Value On A Rest Parameter

查看:319
本文介绍了是否可以在缺省参数设置默认参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ES6引入了一种方便的句法糖。其中包括默认参数功能JavaScript功能,以及休息参数 。尝试在休息参数上设置默认参数值时,我发现我的控制台(或devTools)抱怨( ,引发错误)。我在其他地方很少提到这个特定问题,并且想知道如果 1。)可以这样做, 2。)为什么不(假设是不可能的)



作为一个例子,我已经设计了一个微不足道的(但希望仍然有目的)的例子。在这个函数的第一次迭代中,我已经构建了这样的功能,这样它就可以工作(也就是说,没有给出其余的参数一个默认值)。

  const describePerson =(name,... traits)=>$ {name}!你是$ {traits.join(',')}`; 

describePerson('John Doe','原型占位符');
// => John Doe!你是原型的占位符人

但是,现在默认为: / p>

  const describePerson =(name,... traits = ['a nondescript individual'])=>$ {name}!你是$ {traits.join(',')}`; 

describePerson('John Doe');
// =>未知的语法错误:意外的令牌=

非常感谢任何帮助。

解决方案

不,休息参数不能有默认的初始化。语法不允许,因为初始化程序永远不会运行 - 参数始终被赋予一个数组值(但可能是空值)。



您可以通过

 函数describePerson(name,... traits){
if(traits.length == 0)traits [0] ='一个不简单的个人';
return`$ {name}!你是$ {traits.join(',')}`;
}

  function describePerson(name,firstTrait ='a nondescript individual',... traits){
traits.unshift(firstTrait);
return`$ {name}!你是$ {traits.join(',')}`;
}

//与扩展语法相同的东西:
const describePerson =(name,firstTrait ='a nondescript individual',... otherTraits)=>
`$ {name}!你是$ {[firstTrait,... otherTraits] .join(',')}`


ES6 introduces a bevy of convenient "syntactic sugar". Among them are the default parameter capabilities of JavaScript functions, as well as rest parameters. I'm finding that my console (or devTools) complains (i.e., throws an error) whenever attempting to set a default parameter value on a rest parameter. I've found surprisingly few references to this particular issue elsewhere and am wondering if 1.) it is possible to do so and 2.) why not (assuming it's not possible).

As an example, I've contrived a trivial (but, hopefully still purposeful) example. In this first iteration of the function, I've constructed the function such that it will work (which is to say, without giving the rest parameter a default value).

const describePerson = (name, ...traits) => `Hi, ${name}! You are ${traits.join(', ')}`;

describePerson('John Doe', 'the prototypical placeholder person');
// => "Hi, John Doe! You are the prototypical placeholder person"

However, now with the default:

const describePerson = (name, ...traits = ['a nondescript individual']) => `Hi, ${name}! You are ${traits.join(', ')}`;

describePerson('John Doe');
// => Uncaught SyntaxError: Unexpected token =

Any help is greatly appreciated.

解决方案

No, rest parameters cannot have a default initialiser. It is not allowed by the grammar because the initialiser would never be run - the parameter always gets assigned an array value (but possibly an empty one).

What you want to do could be achieved by either

function describePerson(name, ...traits) {
     if (traits.length == 0) traits[0] = 'a nondescript individual';
     return `Hi, ${name}! You are ${traits.join(', ')}`;
}

or

function describePerson(name, firstTrait = 'a nondescript individual', ...traits) {
     traits.unshift(firstTrait);
     return `Hi, ${name}! You are ${traits.join(', ')}`;
}

// the same thing with spread syntax:
const describePerson = (name, firstTrait = 'a nondescript individual', ...otherTraits) =>
    `Hi, ${name}! You are ${[firstTrait, ...otherTraits].join(', ')}`

这篇关于是否可以在缺省参数设置默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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