Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用未定义的参数覆盖JavaScript默认参数_Javascript_Ecmascript 6 - Fatal编程技术网

使用未定义的参数覆盖JavaScript默认参数

使用未定义的参数覆盖JavaScript默认参数,javascript,ecmascript-6,Javascript,Ecmascript 6,我有一个带有参数bar的函数,它有一个默认参数,“。如何用值未定义的覆盖栏的默认参数 const foo=(bar=”“)=>{ console.log(bar) } foo(null)//null foo(未定义)/“ constfoo=(bar)=>{ console.log(bar) } foo(null)//null foo(未定义)/“可能是因为你不能。这就是为什么默认参数设计用于防止未定义的值 根据Mozilla 默认函数参数允许初始化命名参数 如果未传递值或未定义,则使用默认值 见

我有一个带有参数
bar
的函数,它有一个默认参数,
。如何用值
未定义的
覆盖
栏的默认参数

const foo=(bar=”“)=>{
console.log(bar)
}
foo(null)//null
foo(未定义)/“
constfoo=(bar)=>{
console.log(bar)
}
foo(null)//null

foo(未定义)/“可能是因为你不能。这就是为什么默认参数设计用于防止
未定义的值

根据Mozilla

默认函数参数允许初始化命名参数 如果未传递值或未定义,则使用默认值


见上文。写得很清楚:如果传递了
未定义的
,则使用
默认的
参数。

在您刚才提供的示例代码中,这显然是不可能的

按照

在JavaScript中,函数参数默认为未定义。但是,设置不同的默认值通常很有用。这是默认参数可以提供帮助的地方

为了实现这一点,编写
foo
的合适方法是什么

如果您打算仅在没有参数传递给函数调用时才使用默认值,那么您需要检查
参数的长度,或者如果希望保留arrow函数,则需要分散参数

constfoo=(…args)=>{
常量条=args.length?args[0]:“”;
console.log(bar)
}
foo(null)//null
foo(未定义)//未定义
foo();//“”
不,你不能,这是故意的。 您遇到了一个有趣的JavaScript“两个空”、“空的
null
和未定义的
undefined
的演示

null
是指定的null值

未定义
是指根本没有任何值

您询问传递未定义的“值”的问题,但前提是有缺陷的。没有值
未定义
-
未定义
是缺少值

因此,您不应该将
未定义的
作为有意义的值传递给函数进行解释。我的意思是,你可以,但从JavaScript的角度来看,它相当于什么都不传递——所以你在反对语言的设计,并且会遇到类似这样的问题

如果您想要传递一个有意义的、有目的的空值,这就是
null
的用途。

默认参数的关键是不初始化任何值或使用给定值未定义

因此,您需要删除默认参数,并在函数中添加条件检查(请参阅Kaido的答案)。否则,无法区分foo(未定义)和foo()

如果您想让foo(未定义)和foo()都记录未定义的日志,您可以简单地删除默认参数

给定此方法(typescript,抱歉,我假设es6类似):

用这个称呼:

let u;
let v = undefined;

this.Foo("no parameter");
this.Foo("null", null);
this.Foo("empty", "");
this.Foo("non-empty", "non-empty");
this.Foo("undefined", undefined);
this.Foo("undefined parameter", u);
this.Foo("parameter with value of undefined", v);
我们得到了以下结果:

no parameter 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 1, value: ""}

null 
{bar: null, type: "object", isUndefined: false, arguments: 2, value: null}

empty 
{bar: "", type: "string", isUndefined: false, arguments: 2, value: ""}

non-empty 
{bar: "non-empty", type: "string", isUndefined: false, arguments: 2, value: "non-empty"}

undefined 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 2, value: undefined}

undefined parameter 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 2, value: undefined}

parameter with value of undefined 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 2, value: undefined}
因此,我们可以看到,我们无法区分未定义的变量和包含未定义值的变量之间的区别


通过查看参数的数量,我们可以判断是否缺少参数,然后判断参数是否完全(==)未定义或为null。

这纯粹是学术性的,还是您对此有实际的使用案例?理论上,“如果未传递值或未定义,则允许使用默认值初始化命名参数”是否必须是console.log?使用传递给函数的实际变量的代码是什么?我能看看源代码吗?你能提供真实问题的示例代码吗?实现什么?如果省略默认值,那么它将适用于您发布的代码中的两种情况。那么,在什么情况下,您希望获取空字符串?
未定义的
是默认参数。因此,您的
=undefined
是多余的,第一行与
const foo=(bar)=>{
编辑掉的相同,但其余的对我来说似乎没问题,用户想要某个输出“/”。我们可以很简单地假设OP意识到他们可以删除默认参数,但这显然不是目的。这就像回答“请用“你的名字”来回答“你的名字”。这意味着什么?我认为我们可以合理地假设OP理解默认参数不需要出现在所有参数上。最多,这会改变函数的行为:在OP的版本中,调用
foo()
返回一个空字符串,而您的返回
未定义
。OP询问如何保留空字符串默认参数,同时允许
foo(未定义)
返回
未定义
。问题是如何区分缺少的参数和字面上为
未定义
的指定参数。相关:(12分钟16秒时)-1如果JavaScript是完全正常的,这将是正确的,但事实并非如此,事实也并非如此。正如@kaido的回答所表明的,提供
未定义的
作为参数与完全不提供参数之间绝对有区别,特别是
参数的长度e一个属性,其中“值”"是
未定义的
,而该属性根本不存在。@Chris是的,但从哲学和实用角度来看,我所说的都是真的,如果你把
未定义的
作为一个有意义的值来编写你的代码,你将遇到问题,不得不与语言作斗争。你肯定可以使用某种技巧s来识别是否存在
未定义的
,但在这一点上,您已经深入元编程。在标准用法中,出于实际目的,
未定义的
在功能上完全等同于没有值。顺便说一句,您的对象示例很有趣:-)但我要说的是,这并不是一个反对的观点
no parameter 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 1, value: ""}

null 
{bar: null, type: "object", isUndefined: false, arguments: 2, value: null}

empty 
{bar: "", type: "string", isUndefined: false, arguments: 2, value: ""}

non-empty 
{bar: "non-empty", type: "string", isUndefined: false, arguments: 2, value: "non-empty"}

undefined 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 2, value: undefined}

undefined parameter 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 2, value: undefined}

parameter with value of undefined 
{bar: undefined, type: "undefined", isUndefined: true, arguments: 2, value: undefined}