JavaScript中的函式與命令模式,Java也有參考價值
這篇文章的目的並不是說一種正規化比另一種正規化更好。它只是顯示您遇到的常見模式及其同等的函式實現方法。(banq注:其實對使用Java Stream替代if/else也有參考作用)
if / else
命令正規化:
const hour = 14 let greeting if (hour < 18) { greeting = 'Good day'; } else { greeting = 'Good evening'; } |
函式正規化:
if語句可以被函式getGreeting替代,從而重用:
const isDay = hour => hour < 18 const getGreeting = hour => isDay(hour) ? 'Good Day' : 'Good Evening' const greeting = getGreeting (hour) |
if (無else)
命令正規化:
let name = 'joel' if (name != null) { name = name.toUpperCase() } |
函式正規化:
isNotnull和toUpperCase市可重用的函式:
import when from 'mojiscript/logic/when' const isNotNull = obj => obj != null const toUpperCase = when (isNotNull) (string => string.toUpperCase ()) const name = 'joel' const upperName = toUpperCase (name) |
陣列相加
命令正規化:
const values = [1, 2, 3] let sum = 0 for (const x of values) { sum = sum + x } 函式正規化: 不將sum變成可變變數: |
const values = [1, 2, 3] const add = (x, y) => x + y const sum = values.reduce(add) |
[b]for/if[/b] |
命令正規化:
const values = [1, 2, 3, 4, 5] let evens = for (const x of values) { if (x % 2 === 0) { evens.push(x) } } |
函式正規化:
不要將evens成為可變:
import filter from 'mojiscript/list/filter' const values = [1, 2, 3, 4, 5] const isEven = num => num % 2 === 0 const evens = filter (isEven) (values) |
提前中斷迴圈
命令正規化:
const values = [1, 2, 3] let sum = 0 for (const x of values) { if (x > 3) break sum = sum + x } |
函式正規化:
使用reduceWhile,類似reduce,能接受一個predicate提早結束。
import reduceWhile from 'mojiscript/list/reduceWhile' const add = x => y => x + y const lte3 = num => num <= 3 const sum = reduceWhile (() => lte3) (add) (0) (values) |
if / else if / else
命令正規化:
const fn = temp => { if (temp === 0) return 'water freezes at 0°C' else if (temp === 100) return 'water boils at 100°C' else return `nothing special happens at ${temp}°C` } fn(0) //=> 'water freezes at 0°C' fn(50) //=> 'nothing special happens at 50°C' fn(100) //=> 'water boils at 100°C' |
函式正規化:
import cond from 'mojiscript/logic/cond' import $ from 'mojiscript/string/template' const fn = cond([ <p class="indent">[0, 'water freezes at 0°C'], <p class="indent">[100, 'water boils at 100°C'], <p class="indent">[() => true, $`nothing special happens at ${0}°C`] ]) fn(0) //=> 'water freezes at 0°C' fn(50) //=> 'nothing special happens at 50°C' fn(100) //=> 'water boils at 100°C' |
類
命令正規化:
class Cat { constructor() { this.sound = 'Meow' } talk() { return this.sound } } const cat = new Cat() const talk = cat.talk cat.talk() //=> 'Meow' talk() //=> Error: Cannot read property 'sound' of undefined |
函式正規化:
const cat = { sound: 'Meow' } const dog = { sound: 'Woof' } const talk = animal => animal.sound talk (cat) //=> 'Meow' talk (dog) //=> 'Woof' |
檢查空NULL
命令正規化:
const toUpper = string => { if (string != null) { return string.toUpperCase() } } |
函式正規化:
此示例將引數包裝在一個
Maybe |
import S from 'sanctuary' const toUpper = S.pipe ([ S.toMaybe, S.map (string => string.toUpperCase ()), S.maybeToNullable ]) // If you use `Maybe` throughout your app, this would be your `toUpper` function. const toUpper = S.map (string => string.toUpperCase ()); |
相關文章
- 2.13 靜默模式DBCA命令參考模式
- js 函式中形參與實參的關係JS函式
- TypeScript 中函式的理解?與 JavaScript 函式的區別?TypeScript函式JavaScript
- React中this值繫結和事件函式傳參React事件函式
- Python的函式形參和返回值Python函式
- php中函式的實參和形參PHP函式
- 函式形參與實參的體會與對比函式
- JavaScript中的compose函式和pipe函式JavaScript函式
- LVS - ipvsadm命令參考
- Oracle ASMCMD命令參考OracleASM
- Redis(四)--- Redis的命令參考Redis
- JavaScript的迭代函式與迭代函式的實現JavaScript函式
- JavaScript | 函式與方法JavaScript函式
- JavaScript 設計模式系列 – 自定義函式(惰性函式)JavaScript設計模式函式
- javascript函式中with的介紹JavaScript函式
- JavaScript中的函式繼承JavaScript函式繼承
- JavaScript物件參考手冊JavaScript物件
- JavaScript Math(數學) 參考JavaScript
- javascript 的函式宣告與表示式對比JavaScript函式
- JDK11的工具的命令參考JDK
- JavaScript:鉤子函式與回撥函式的區別JavaScript函式
- JavaScript函式引數解構賦值JavaScript函式賦值
- Redis命令參考之INFO命令詳解Redis
- javascript學習筆記--函式的返回值可以是一個函式JavaScript筆記函式
- JAVA中的Random()函式Javarandom函式
- 理解 JavaScript 中的高階函式JavaScript函式
- (譯) JavaScript中的組合函式JavaScript函式
- 對JavaScript中函式物件的理解JavaScript函式物件
- javascript中generator函式的介紹JavaScript函式
- 函式呼叫的代價與優化函式優化
- 7 Oracle DataGuard 命令列參考Oracle命令列
- 【XTTS】 xttdriver.pl命令參考TTS
- 【TUNE_ORACLE】列出帶有自定義函式的SQL的SQL參考Oracle函式SQL
- 強化學習 6 ——價值函式逼近強化學習函式
- python函式的入參和傳參Python函式
- Golang 中函式作為值與型別(轉載)Golang函式型別
- EventEmitter:從命令式 JavaScript class 到宣告函式式的華麗轉身MITJavaScript函式
- 函式中引數傳值函式