<!DOCTYPE html>
<html>
<head>
<title>对象解构赋值与函数参数自动解构对象语法中的参数默认值设置方法</title>
</head>
<body>
<script type="text/javascript">
//1.对象的解构赋值
// var {age,sex,say} = {"age":30, "sex":"男", "say":function(){return "hello";}};
//或者
// var obj = {"age":30, "sex":"男", "say":function(){return "hello";}};
// var {age,sex,say} = obj;
//2.函数参数使用对象解构
// function f1({age,sex,say}){
// console.log(age, sex, say);
// }
// var obj = {"age":30, "sex":"男", "say":function(){return "hello";}};
// f1(obj);
//3.设置默认值 整体默认为{}
// function f2({age,sex,say}={}){
// console.log(age, sex, say);
// }
// var obj = {"age":30, "sex":"男", "say":function(){return "hello";}};
// f2(obj);
// f2();
//4.设置默认值 整体默认一个对象
// function f3({age,sex,say}={"age":20,"sex":"男"}){
// console.log(age, sex, say);
// }
// var obj = {"age":30, "sex":"男", "say":function(){return "hello";}};
// f3(obj);
// f3();
//5.设置默认值 整体默认为{},并给每个变量单独设置默认值
function f4({age=30,sex="女",say}={}){
console.log(age, sex, say);
}
var obj = {"age":30, "sex":"男", "say":function(){return "hello";}};
f4(obj);
f4();
</script>
</body>
</html>
|