这是第10篇笔记!
让学习“上瘾”,成为更好的自己!!!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用途</title>
</head>
<body>
<script>
// 1, 交换变量的值
let x = 1;
let y = 2;
[x, y] = [y, x];
console.log(x);
console.log(y);
// 2, 从函数返回多个值
// 返回一个数组
// function example1(){
// return [1, 2, 3];
// }
// let [a, b, c] = example1();
// 返回一个对象
function example2(){
return {
foo: 1,
bar: 2
};
}
let {foo, bar} = example2();
// 3, 函数参数的定义 --> 解构赋值可以方便地将一组参数与变量名对应起来
// 参数是一组有次序的值
function f1([x, y, z]){
return x+y+z;
}
console.log(f1([1, 2, 3]));
// 参数是一组无次序的值
function f2({x, y, z}){
return x+y+z;
}
console.log(f2({x: 2, y: 23, z: 1}));
// 4, 提取JSON数据
let jsonData = { // 注意,这不是Json数据格式的书法!
id: 42,
status: "ok",
data: [898, 787]
};
let {id, status, data} = jsonData;
console.log(id, status, data); // 42 ,"ok" ,[898, 787]
// 5, 函数参数的默认值 --> 避免在函数体内再写“var foo = config.foo || 'default foo' ”
// jQuery.ajax = function(url, {
// async = true,
// beforeSend = function(){},
// cache = true,
// complete = function(){},
// crossDomain = false,
// global = true,
// // ...
// }){
// // do stuff
// };
// 6, 遍历Map结构
// 任何部署了Iterator结构的对象都可以用“for...of”循环遍历
// Map结构原生支持Iterator接口,配合变量的结构赋值获取键名和键值很方便
var map = new Map();
map.set('first', 'hello');
map.set('second', 'world');
console.log(map);
for(let [key, value] of map){
console.log(key + ' is ' + value);
}
// 7, 输入模块的指定方法
// 加载模块时候,往往需要制定输入的方法
// const {SourceMapConsumer, SourceNode} = require('source-map');
</script>
</body>
</html>