我需要一些帮助来理解JavaScript中的闭包。我需要创建一个函数(function1),用于在数组元素之间插入指定的分隔符,如果没有指定分隔符,则插入逗号。我试过了,但不起作用。
function function1(separator)
{
return function(elements)
{
for (var i = 0; i < elements.length; i++)
return (`${elements}`+ separator);
};
}
var ex1 = function1("/");
ex1("One");
ex1("Two");
var ex2 = function1("*");
ex2("One");
ex2("Two");
var ex3 = function1();
ex3("One");
ex3("Two");
console.log("ex1 is" + ex1() );
console.log("ex2 is " + ex2() );
console.log("ex3 is " + ex3() );
输出应为
ex1 is One/Two
ex2 is One*Two
ex3 is One,Two
https://stackoverflow.com/questions/52959441
复制相似问题