if (n === null && o !== null || // non null -> null
n !== null && o === null || // null -> non null
(n !== null && o !== null && // both non null, check members
(n.age !== o.age ||
n.name !== o.name))) {
triggerEvent();
}
讓我們打破這(我用n
和o
速記,以適應更多的線,使其更容易,看看有什麼GOI納克上 - 不是建議使用這種簡潔約定):
n === null && o !== null ||
n !== null && o === null
此表達式可以僅僅簡化:n !== o
。
這是有效的,因爲如果一個爲null而不是另一個,則表達式將評估true。如果對象指向相同的內存地址,這也將返回false(這是一件好事,因爲它們的字段也將匹配,並且在這種情況下我們不希望觸發事件)。
這樣可以讓我們在這裏:
if (n !== o ||
((n !== null && o !== null) &&
(n.age !== o.age || n.name !== o.name))) {
triggerEvent();
}
爲了簡化超出了通常需要引入的功能。如果你爲這些狀態提供了一種平等方法,比較這些age
和name
字段,可能會很不錯。如果你添加了新的狀態,它也會使代碼更容易出錯。
空交換技巧
另一個技巧,有時可以在幾個週期的成本是有用的對之間的空掉。例如:
// if 'a' is null:
if (!a)
{
// swap it with 'b'.
a = [b, b = a][0];
}
// Now if 'a' is still null, both values are null.
// If 'b' is not null, both values are not null.
我們可以利用它在上面的代碼如下所示:
local state1 = oldState
local state2 = newState
if (!state1)
state1 = [state2, state2 = state1][0];
if (state1) {
// If only one of these is not null or their fields don't match:
if (!state2 || state1.age !== state2.age || state1.name !== state2.name) {
triggerEvent();
}
}
很多,這是否可以簡化或complexifies將根據你如何經常使用這種簡短交換單的襯墊。如果非常罕見,那可能只會增加混亂。但是這種交換技術可以讓你編寫稍微冗長的代碼來交換可能更直接的代碼,並且不會讓你的大腦堆棧淹沒(有時候更簡單的代碼比一些非常複雜的東西更需要嵌套非常人性的解析)。
從我的頭頂開始,您可以嘗試在兩個對象上使用'JSON.stringify'並將它們簡單地作爲字符串進行比較。 ((newState || newState)&&(!oldState || oldState)&&(newState && oldState)){if(newState.name!== oldState.name){ triggerEvent(){ – 1cgonza
可以試試這個 ' ; } }' –
@ 1cgonza但'JSON.stringify'是否會說出關於'Object'中的鍵的順序?而且即使在ES6中按鍵排序的時候,你是否真的希望「{a:1,b:2}」和「{b:2,a:1}」不相等? –