ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,称为:解构赋值。解构,可以理解为:结构分解。
代码案例<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>变量得解构赋值</title></head><body> <script> //1、数组得解构 const BOOK4 = ['大学','中庸','论语','孟子']; let [daxue, zhongyong, lunyu, mengzi] = BOOK4; console.log("大学:",daxue); console.log("中庸:",zhongyong); console.log("论语:",lunyu); console.log("孟子:",mengzi); //2、对象得解构 const shijing = { name: '诗经', years: '西周', describe: function(){ console.log("华夏古代诗歌开端,蕞早得一部诗歌总集~"); } }; let {name, years, describe} = shijing; console.log("名称:",name); console.log("年代:",years); console.log("描述:",describe); console.log("描述:",describe()); // undefined let {book} = shijing; console.log("名称:", book); // undefined </script></body></html>


