<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// Aarry.from( )还接收第二个可选的映射函数参数。这个参数可以直接增强新数组的值,而无需像调用Array.from( ).map( )那样先创建一个中间数组。
// 还可以接收第三个可选参数,用于指定映射函数中的this的值。但这个重写的this值在箭头函数中不适用。
const a1 = [1, 2, 3, 4];
const a2 = Array.from(a1, x => x ** 2);
const a3 =
Array.from(a1, function (x) { return x ** this.exponent }, { exponent: 3 });//第三个就是一个对象,它为第二个函数的this指向
console.log(a2); // [1, 4, 9, 16]
console.log(a3); // [1, 8,27, 64]
let array = [1, 2, 3, 5];
// array[6]=11;
// console.log(array);
// console.log(array[4]);//undefinde
//console.log(array.forEach(()=>"中国"));//undefinde
// array.forEach(function (x,index,arr) {//引用地址指向同一个地址
// arr[index] = "中国";
// })
// console.log(array)//[ "中国", "中国", "中国", "中国" ]
// let ay = array.map(()=>'中国')
// console.log(ay);//[ "中国", "中国", "中国", "中国" ]
let ay = array.map(function(){
return '中国';
})
console.log(ay);//[ "中国", "中国", "中国", "中国" ]
// let new_array = Array.from(array,function(){
// return '中国'//箭头函数单条处理可以省略return和{大括号}
// });
// console.log(new_array)//[ "中国", "中国", "中国", "中国" ]
let array = [1, 2, 3, 5];
let new_array = Array.from(array,()=>'中国');
console.log(new_array)//[ "中国", "中国", "中国", "中国" ]
// array.forEach(function(valve){
// valve=valve+1;
// console.log(valve);//有
// })
// console.log(array)//未改变
</script>
</html>
记录一下我对map()和Array.from()的实验【2021.11.23】
最新推荐文章于 2022-09-20 17:22:15 发布