鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ vue中,同步執(zhí)行的問題

vue中,同步執(zhí)行的問題

vue中,有以下代碼,其中,arr是放在vuex中,

arr={"costPrice":"","jdPrice":"","sellerSkuId":"","skuId":null,"skuName":"Dyy-test-451331  ","stock":"","netWeight":"","packHeight":"","packLong":"","packWide":"","piece":0,"textAttrValueAlias":"BLACK","textAttrValueId":2258240,"upc":"","stockVOs":[{"dcId":"","dcName":"","storeId":"","storeName":""},{"dcId":"","dcName":"","storeId":"","storeName":""},{"dcId":"","dcName":"","storeId":"","storeName":""},{"dcId":"","dcName":"","storeId":"","storeName":""},{"dcId":"","dcName":"","storeId":"","storeName":""}],"textShowFlag":true}

然后,更改arr

console.log(arr)
console.log(JSON.stringify(arr))
console.log(arr.textShowFlag)
Vue.set(this.skuListGroupVOs[rowNumber].childList[columnNumber], 'textShowFlag', !arr.textShowFlag)

為什么會出現(xiàn)JSON.stringify前后不一致的問題?
clipboard.png

回答
編輯回答
脾氣硬

textShowFlag原本本就是false
打印的時候arr中的textShowFlag也是false,但是后面你執(zhí)行了取反操作,arr中的textShowFlag變成了true
注意,此時你再看是true,因?yàn)榇蛴〉氖且?,點(diǎn)開之后是是取反后的最終值
你可以debugger看一下,此時arr點(diǎn)開的textShowFlag是false。

console.log(arr)
console.log(JSON.stringify(arr))
console.log(arr.textShowFlag)
debugger
Vue.set(this.skuListGroupVOs[rowNumber].childList[columnNumber], 'textShowFlag', !arr.textShowFlag)
2018年3月14日 18:53
編輯回答
巷尾

瀏覽器 為了 性能,在console.log時候 僅僅只會保存對象的引用,所以console.log輸出的并不是當(dāng)前的值
很可能是異步之后的 對象值
而 JSON.stringify 把 這個變?yōu)榱俗址淮嬖?字符串改變的情況,僅此而已

2017年2月19日 21:04