<!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>解构语法</title>
</head>
<body>
<script>
// 立方体对象
var c1 = { length: 40, width: 30, height: 100 }
// 传参模式
function volume(cube) {
// 返回体积
// return cube.length * cube.width * cube.height
var { length, width, height } = cube
return length * width * height
}
// 面积 = (长x宽 + 长x高 + 宽x高)*2
function area(cube) {
// 返回结果:
// return (cube.length * cube.width + cube.length * cube.height + cube.width * cube.height) * 2
var { width: w, length: l, height: h } = cube
return (w * l + w * h + l * h) * 2
}
// 形参解构语法: 直接在形参位置写解构
function area({ width: w, length: l, height: h }) {
return (w * l + w * h + l * h) * 2
}
var c2 = { length: 10, width: 20, height: 30 }
console.log(area(c1));
console.log(area(c2));
console.log(volume(c1))
var r1 = { x: 10, y: 20 }
function add({ x, y }) {
return x + y
}
console.log(add(r1));
</script>
</body>
</html>