三、变量的解构赋值

郁子小于 1 分钟约 151 字笔记ECMAScript尚硅谷李强

  • ES6 允许按照一定模式从数组和对象中提取值,对变量进行赋值 —— 解构赋值

(一)数组的解构

const KING = ["沈腾", "贾玲", "华晨宇", "关晓彤"];
let [shen, jia, hua, guan] = KING;
console.log(shen);
console.log(jia);
console.log(hua);
console.log(guan);

(二)对象的解构

const shenteng = {
  name: "沈腾",
  age: "不详",
  xiaopin: function () {
    console.log("我可以演小品。");
  },
};

1.属性解构

let { name, age, xiaopin } = shenteng;
console.log(name);
console.log(age);
console.log(xiaopin);
xiaopin();

2.方法解构

// shenteng.xiaopin();
// shenteng.xiaopin();
// shenteng.xiaopin();
// shenteng.xiaopin();

// 推荐
let { xiaopin } = shenteng;
xiaopin();
上次编辑于: