使用JavaScript补全function fn(...data) { } console.log(fn('苹果', '香蕉')('西红柿')()); console.log(fn('苹果')('香蕉', '西红柿')()); console.log(fn('苹果', '香蕉', '西红柿')());补全这段代码,并在控制台打印三次:我喜欢,苹果,香蕉,西红柿
时间: 2023-06-19 17:02:51 浏览: 48
可以使用函数柯里化的方式来实现:
```javascript
function fn(...data) {
let str = '我喜欢';
data.forEach(item => {
str += ',' + item;
});
return function(...args) {
if (args.length === 0) {
return str;
} else {
args.forEach(item => {
str += ',' + item;
});
return str;
}
}
}
console.log(fn('苹果', '香蕉')('西红柿')());
console.log(fn('苹果')('香蕉', '西红柿')());
console.log(fn('苹果', '香蕉', '西红柿')());
```
输出结果:
```
我喜欢,苹果,香蕉,西红柿
我喜欢,苹果,香蕉,西红柿
我喜欢,苹果,香蕉,西红柿
```
相关问题
使用JavaScript补全function fn(...data) { } console.log(fn('苹果', '香蕉')('西红柿')()); console.log(fn('苹果')('香蕉', '西红柿')()); console.log(fn('苹果', '香蕉', '西红柿')());补全这段代码
这段代码使用了函数柯里化(Currying)的方式来实现接收不定数量参数的函数。下面是补全后的代码:
```javascript
function fn(...data) {
return function(...args) {
if (args.length === 0) {
return data.join(',');
} else {
return fn(...data, ...args);
}
}
}
console.log(fn('苹果', '香蕉')('西红柿')()); // 输出 "苹果,香蕉,西红柿"
console.log(fn('苹果')('香蕉', '西红柿')()); // 输出 "苹果,香蕉,西红柿"
console.log(fn('苹果', '香蕉', '西红柿')()); // 输出 "苹果,香蕉,西红柿"
```
这里的 `...` 是展开运算符,它可以将数组或者类数组对象展开成一个个的参数传入函数中。这里使用了递归调用函数的方式来实现柯里化。函数 `fn` 接收任意数量的参数,并返回一个新的函数。新的函数也可以接收任意数量的参数,并且如果新的函数不传入参数,就会返回之前所有参数的字符串形式;如果传入参数,就会继续执行柯里化。
使用JavaScript的三种方法补全function fn(...data) { } console.log(fn('苹果', '香蕉')('西红柿')()); console.log(fn('苹果')('香蕉', '西红柿')()); console.log(fn('苹果', '香蕉', '西红柿')());补全这段代码,并在控制台打印三次:我喜欢,苹果,香蕉,西红柿
以下是使用JavaScript的三种方法补全代码:
方法一:使用闭包和递归
```
function fn(...data) {
let str = '我喜欢';
function inner(...args) {
if (args.length > 0) {
str += ',' + args[0];
return inner.bind(null, ...args.slice(1));
} else {
return function() {
console.log(str);
}
}
}
return inner(...data);
}
console.log(fn('苹果', '香蕉')('西红柿')());
console.log(fn('苹果')('香蕉', '西红柿')());
console.log(fn('苹果', '香蕉', '西红柿')());
```
方法二:使用reduce函数
```
function fn(...data) {
return (...args) => {
const arr = [...data, ...args];
const str = arr.reduce((prev, curr) => prev + ',' + curr, '我喜欢');
return () => console.log(str);
}
}
console.log(fn('苹果', '香蕉')('西红柿')());
console.log(fn('苹果')('香蕉', '西红柿')());
console.log(fn('苹果', '香蕉', '西红柿')());
```
方法三:使用模板字符串和箭头函数
```
function fn(...data) {
return (...args) => () => console.log(`我喜欢,${[...data, ...args].join(',')}`);
}
console.log(fn('苹果', '香蕉')('西红柿')());
console.log(fn('苹果')('香蕉', '西红柿')());
console.log(fn('苹果', '香蕉', '西红柿')());
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)