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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

炎炎設(shè)計 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), 有個關(guān)于slice()的疑問: 為什么要復(fù)制一份數(shù)據(jù)呢? 使用slice()復(fù)制出來的數(shù)據(jù)對比差異還是要全部遍歷
查看完整描述

1 回答

?
ITMISS

TA貢獻1871條經(jīng)驗 獲得超8個贊

文檔里說了,如果你不做immutable的話,你就沒辦法知道有沒有改變,因為你改的是對象,舉個簡單的例子說明:

function blackbox(arr) {

   /**

    * 在這里可以對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...直接對比,沒辦法比較

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

而如果

function blackbox(arr) {

  if(我要改變) {

    let result = arr.slice();

    /**

    * 在這里可以對result直接做任何處理

    */

    return result;

  } else return arr;  

}

這個樣子:

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


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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