例如,我不知道如何获取参数名称:
function func(sep='\n') {
var args = Array.prototype.slice.call(arguments);
}
func("Arg1", "Argument2", "Something", sep="MySeparator")
// I want to get value of "sep" in arguments of function
// In this example it should set optional parameter "sep" to "MySeparator"
任何人都可以帮忙吗?
我不确定我是否正确理解了这个问题,但是好像您想实现这样的事情:
function func(separator = '\n', ...args) {
return args.join(separator);
}
console.log(func(' - MySeparator - ', 'Arg1', 'Arg2', 'Arg3'));
func
中的第一个参数接受您要使用的分隔符,而所有其他参数将作为数组传递到函数中,您可以在其中使用join
将它们与分隔符粘合在一起。