[英]Converting object literals into functions in ES6 Javascript
我在hackernoon.com 閱讀了這篇文章,它試圖解釋一種將javascript Switch/Case 變成它自身的功能版本的特定方法。
文章達到了一個點,他們設置了一個名為 switchcase 的switchcase
,定義為:
const switchcase = cases => defaultCase => key =>
cases.hasOwnProperty(key) ? cases[key] : defaultCase
他們還說這個 function 有一個問題,因為“整個 object 文字在傳遞給 switchcase 函數之前已經過評估”。 因此他們決定將 object 文字中的值轉換為函數。 為此,他們使用以下語法:
const switchcaseF = cases => defaultCase => key =>
switchcase(cases)(defaultCase)(key)()
我的問題是:最后一個語法是如何工作的? 誰能幫我分解一下?
通過使用函數,您還需要為defaultCase
取一個 function。
const switchcase = cases => defaultCase => key => cases.hasOwnProperty(key)? cases[key]: defaultCase const switchcaseF = cases => defaultCase => key => switchcase(cases)(() => defaultCase)(key)() // ^^^^^^^^^^^^^^^^^ ^^ console.log(switchcaseF({ foo: () => 'bar' })('nothing')('foo')); console.log(switchcaseF({ foo: () => 'bar' })('nothing')('baz'));
如果用簡單的 function 重寫它,它將被清除。
首先,重寫switchcase
。 這個 function 是用三個 arguments 柯里化下一個 function 的結果
function switchcase(cases, defaultCase, key){
if(cases.hasOwnProperty(key)){
return cases[key];
}
return defaultCase;
}
所以,如果重寫switchcaseF
我們得到下一個:
function switchcaseF(cases, defaultCase, key){
var selectedFunc = switchcase(cases, defaultCase, key);
return selectedFunc();
}
最后一個語法是如何工作的? 誰能幫我分解一下?
考慮switchcase
和switchcaseF
的定義。
const switchcase = cases => defaultCase => key =>
cases.hasOwnProperty(key) ? cases[key] : defaultCase
const switchcaseF = cases => defaultCase => key =>
switchcase(cases)(defaultCase)(key)()
如果我們在switchcase
中內聯switchaseF
的應用程序,我們會得到以下結果。
const switchcaseF = cases => defaultCase => key =>
(cases.hasOwnProperty(key) ? cases[key] : defaultCase)()
//|____________________________________________________|
// |
// switchcase(cases)(defaultCase)(key)
此外,我們可以將 function 應用程序移動到條件表達式中。
const switchcaseF = cases => defaultCase => key =>
cases.hasOwnProperty(key) ? cases[key]() : defaultCase()
現在,考慮您鏈接到的文章中的示例。
const counter = (state = 0, action) =>
switchcaseF({
'INCREMENT': () => state + 1,
'DECREMENT': () => state - 1
})(() => state)(action.type)
如果我們在counter
中內聯switchcaseF
的應用程序,我們會得到以下結果。
const counter = (state = 0, action) => {
const cases = {
'INCREMENT': () => state + 1,
'DECREMENT': () => state - 1
}
const defaultCase = () => state
const key = action.type
return cases.hasOwnProperty(key) ? cases[key]() : defaultCase()
}
因此,如果action.type
是'INCREMENT'
那么結果是state + 1
。 如果action.type
是'DECREMENT'
那么結果是state - 1
。 否則,結果為state
。
我們寫() => state + 1
而不是簡單的state + 1
的原因是為了惰性求值。 我們只在調用 function 時評估() => state + 1
的主體。 這可以防止不正確的行為,如下例所示。
const switchcase = cases => defaultCase => key => cases.hasOwnProperty(key)? cases[key]: defaultCase const never = () => { while (true); } const example = key => switchcase({ never: never() })('it works')(key) console.log(example('it should work')) // expected 'it works' but never returns
使用switchcaseF
解決了這個問題。
const switchcaseF = cases => defaultCase => key => cases.hasOwnProperty(key)? cases[key](): defaultCase() const never = () => { while (true); } const example = key => switchcaseF({ never: () => never() })(() => 'it works')(key) console.log(example('it should work')) // 'it works' as expected
但是,請注意,您也可以使用 getter 使其與switchcase
一起使用。
const switchcase = cases => defaultCase => key => cases.hasOwnProperty(key)? cases[key]: defaultCase const never = () => { while (true); } const example = key => switchcase({ get never() { return never(); } })('it works')(key) console.log(example('it should work')) // 'it works' as expected
我們也可以讓defaultCase
變得懶惰。
const switchcase2 = cases => defaultCase => key => cases.hasOwnProperty(key)? cases[key]: defaultCase.value const never = () => { while (true); } const example = key => switchcase2({ get never() { return never(); } })({ get value() { console.log('yes'); return 'it works'; } })(key) console.log(example('it should work')) // 'yes' 'it works' as expected
如果您不希望它變得懶惰,那么您可以將其strict
包裝如下。
const switchcase2 = cases => defaultCase => key => cases.hasOwnProperty(key)? cases[key]: defaultCase.value const never = () => { while (true); } const strict = value => ({ value }) const example = key => switchcase2({ get never() { return never(); } })(strict('it works'))(key) console.log(example('it should work')) // 'it works' as expected
希望能解開你的疑惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.