亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線(xiàn)咨詢(xún)客服
新站提交
  • 網(wǎng)站:52010
  • 待審:67
  • 小程序:12
  • 文章:1106242
  • 會(huì)員:784

孤立地學(xué)習(xí)新主題,否則頭腦將無(wú)法長(zhǎng)期完全掌握這個(gè)概念。這也得到了一些實(shí)證研究的支持。
解構(gòu):將數(shù)組或?qū)ο笾械闹到獍絾为?dú)變量中的方法。

const nums = [8,4,5];
const num1 = nums[0];
const num2 = nums[1];
const num3 = nums[2];
console.log(num1, num2, num3);

is reduced to 

const [x,y,z] = nums;
console.log(x, y, z);

three const variables named x,y,z are created in this step

登錄后復(fù)制

[x,y,z] 雖然看起來(lái)像一個(gè)數(shù)組,但是當(dāng)它位于 = 的 lhs 上時(shí),則被視為解構(gòu)。
解構(gòu)是不可變的操作。

const girl = {
  name: 'melania',
  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya'],
  eats: ['roasted', 'pizza', 'burger', 'rice', 'manchurian'],
};

const [first, second] = girl.friends;
console.log(first, second);

const [,,,fourth,last] = girl.eats;
console.log(fourth, last);

登錄后復(fù)制

交換變量[變異]

let array = [5,6];
let [a,b] = array;
console.log(`a: ${a}, b:${b}`);
[b,a] = [a,b];
console.log(`a: ${a}, b:${b}`);

登錄后復(fù)制

函數(shù)返回一個(gè)數(shù)組,立即破壞結(jié)果,這允許我們從函數(shù)返回多個(gè)值

const girl = {
  name: 'melania',
  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya'],
  eats: ['roasted', 'pizza', 'burger', 'rice', 'manchurian'],
  drinks: ['juice','coffee','coke'],
  order: function(eat,drink){
    return [this.eats[eat],this.drinks[drink]];
  }
};

const [maincourse, drinks] = girl.order(2, 2);

console.log(`maincourse: ${maincourse}`);
console.log(`drinks: ${drinks}`);

登錄后復(fù)制

解構(gòu)嵌套數(shù)組

let nums = [5,3,[8,7,9,3]];
let [x,y,z] = nums;
console.log(`x: ${x}`); // 5
console.log(`y: ${y}`); // 3
console.log(`z: ${z}`); // 8,7,9,3

let nums2 = [5,3,[8,7]];
let [x,,[y,z]] = nums2;
console.log(`x: ${x}`, `y: ${y}`, `z: ${z}`); // 5 8 7 

登錄后復(fù)制

從未知大小的數(shù)組解構(gòu):

const names = ['michael','charlie','peter'];
let [w='xxx',x='xxx',y='xxx',z='xxx'] = names;
console.log(w,x,y,z); // 'michael' 'charlie' 'peter' 'xxx'

登錄后復(fù)制

解構(gòu)對(duì)象:

使用 {} 進(jìn)行對(duì)象解構(gòu),使用 [] 進(jìn)行數(shù)組解構(gòu)。
提供要提取的對(duì)象屬性名稱(chēng)中提到的準(zhǔn)確變量名稱(chēng)。這些變量名稱(chēng)的順序并不重要。

const girl = {
  name: 'melania',
  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya'],
  eats: ['roasted', 'pizza', 'burger', 'rice', 'manchurian'],
  drinks: ['juice','coffee','coke'],
  works: {
        mtwt: {
          start: 9,
          end: 5
        },
        fri: {
          start:9,
          end: 3
        }
  }
};

const {name, works, drinks} = girl;
console.log(name);
console.log(works);
console.log(drinks);


// replace long property names with custom names:
const {name:user, works:timings, drinks:enjoys} = girl;
console.log(user);
console.log(timings);
console.log(enjoys);


//destructuring data from api calls returned in the form of objects i.e attaching a default value to a property that does not exist on object received from an api call

// details does not exist, so default value is assigned
const { details = [], eats: loves = [] } = girl;
console.log(details);
// eats exist but is renamed as loves, hence default value won't apply
console.log(loves);

登錄后復(fù)制

## mutating variables using object destructuring
let x = 10;
let y = 20;
let obj = {x:1, y:2, z:3};

{x,y} = obj; // error
when we start a line with a '{', then js expects a code-block. and we cannot assign anything to a code-block on lhs using = operator. hence, an error is thrown. the error is resolved by wrapping into () as shown below
({x,y} = obj); //{ x: 1, y: 2, z: 3 }

登錄后復(fù)制

解構(gòu)嵌套對(duì)象

const girl = {
  name: 'Melania',
  friends: ['Alina', 'Alice', 'Ayesha', 'Anamika', 'Anaya'],
  eats: ['Roasted', 'Pizza', 'Burger', 'Rice', 'Manchurian'],
  drinks: ['Juice','Coffee','Coke'],
  works: {
        mtwt: {
          start: 9,
          end: 5
        },
        fri: {
          start:10,
          end: 2
        }
  }
};

let { fri } = works;
console.log(fri);

// Destructuring the fri object using the same property names start, end
let {fri: {start, end}} = works;
console.log(start, end);

// Further renaming for shortening start as 'b' and end as 'e'
let {fri: {start: b, end: e}} = works;
console.log(b, e);





const girl = {
  name: 'Melania',
  friends: ['Alina', 'Alice', 'Ayesha', 'Anamika', 'Anaya'],
  eats: ['Roasted', 'Pizza', 'Burger', 'Rice', 'Manchurian'],
  drinks: ['Juice','Coffee','Coke'],
  works: {
        mtwt: {
          start: 9,
          end: 5
        },
        fri: {
          start:10,
          end: 2
        }
  },
  // these destructured property-names have to be same as they are passed inside the girl.sleep(). Order need not be same.
  sleep: function ({time='NA', address='NA', color = 'NA', duration='NA'}){
    console.log(`${this.name} sleeps at ${address} for ${duration} in ${color}light for ${duration}. She loves to eat ${this.eats[0]}`);
  }
};

// A single object is passed, which will be destructured by the method inside the object extracting all values via destructuring
girl.sleep({time: '10pm', address:'home', color: 'blue', duration: '7hrs'});

girl.sleep({time: '9pm', duration: '7hrs'});

登錄后復(fù)制

分享到:
標(biāo)簽:實(shí)時(shí) 對(duì)象 數(shù)組 文檔 解構(gòu)
用戶(hù)無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 52010

    網(wǎng)站

  • 12

    小程序

  • 1106242

    文章

  • 784

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定