-
function foo(...value) {
-
console.log(value);
-
}
-
foo
(2,4,5);
它传递进来实际上是一个数组
所以是可以进行遍历的
-
function foo(...value) {
-
console.log(value);
-
value.forEach(
function (item,index) {
-
console.log(item,index);
-
})
-
}
-
foo(
2,
4,
5);
若有参数是固定的,那么传参的方式,固定参数在前可变参数在后
-
function foo(a,...value) {
-
console.log(value);
-
value.forEach(
function (item,index) {
-
console.log(item,index);
-
})
-
}
-
foo(
2,
4,
5);
由于2变成了固定参数,所以遍历的时候只遍历4、5
数组合并处理:
-
let arr1 = [
2,
3];
-
let arr2 = [
1,...arr1,
4];
-
console.
log(arr2);
-
console.
log(...arr1);
完整代码:
-
<!DOCTYPE html>
-
<html lang="en">
-
<head>
-
<meta charset="UTF-8">
-
<title>Title
</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
// function foo(a,...value) {
-
// console.log(value);
-
// value.forEach(function (item,index) {
-
// console.log(item,index);
-
// })
-
// }
-
// foo(2,4,5);
-
-
let arr1 = [
2,
3];
-
let arr2 = [
1,...arr1,
4];
-
console.log(arr2);
-
console.log(...arr1);
-
</script>
-
</body>
-
</html>
转载:https://blog.csdn.net/Stubborn_bull/article/details/117228331
查看评论