第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

react 引導(dǎo)中關(guān)于"Immutability"的疑問

react 引導(dǎo)中關(guān)于"Immutability"的疑問

炎炎設(shè)計(jì) 2018-11-15 16:15:17
https://facebook.github.io/re...handleClick(i) {    const squares = this.state.squares.slice();     squares[i] = 'X';    this.setState({squares: squares});   }Determining if a mutated object has changed is complex because changes are made directly to the object. This then requires comparing the current object to a previous copy, traversing the entire object tree, and comparing each variable and value. This process can become increasingly complex.Determining how an immutable object has changed is considerably easier. If the object being referenced is different from before, then the object has changed. That's it.以上摘自React的引導(dǎo), 有個(gè)關(guān)于slice()的疑問: 為什么要復(fù)制一份數(shù)據(jù)呢? 使用slice()復(fù)制出來的數(shù)據(jù)對(duì)比差異還是要全部遍歷
查看完整描述

1 回答

?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊

文檔里說了,如果你不做immutable的話,你就沒辦法知道有沒有改變,因?yàn)槟愀牡氖菍?duì)象,舉個(gè)簡(jiǎn)單的例子說明:

function blackbox(arr) {

   /**

    * 在這里可以對(duì)arr直接做任何處理,也可以不做任何處理

    * 比如:arr[Math.floor(Math.random() * arr.length)] = 2;

    */

   return arr;

}


var array = [5,21,54,43,542,213,43,6,2];

var result = blackbox(arr);


// 我想知道新的result是否發(fā)生了改變,該怎么辦?

console.log(array === result); // true...直接對(duì)比,沒辦法比較

console.log(array.join() === result.join()); // true ...對(duì)比內(nèi)容,沒辦法知道

而如果

function blackbox(arr) {

  if(我要改變) {

    let result = arr.slice();

    /**

    * 在這里可以對(duì)result直接做任何處理

    */

    return result;

  } else return arr;  

}

這個(gè)樣子:

var array = [5,21,54,43,542,213,43,6,2];

var result = blackbox(arr);


// 我想知道新的result是否發(fā)生了改變,該怎么辦?

console.log(array === result); // 如果發(fā)生改變,那么就是false。不改變,是true

console.log(array.join() === result.join()); // 如果發(fā)生改變,那么就是false。不改變,是true


查看完整回答
反對(duì) 回復(fù) 2018-12-31
  • 1 回答
  • 0 關(guān)注
  • 495 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)