Oggetto letterale, invece di passare

Esempi di codice

0
0

Non Usare If-Else e Switch in JavaScript, Utilizzo di Oggetti Letterali

const getValue = (value) => ({
  [value == 1]: 'Value is 1',
  [value > 1]: 'Value is greater than 1',
  [value < 1]: 'Value is less than 1',
})[true]

console.log(getValue(2));
console.log(getValue(-1)); 
console.log(getValue(-1+2)); // expect 'Value is 1'
0
0

oggetto javascript invece di passare

const dogSwitch = (breed) => ({
  "border": "Border Collies are good boys and girls.",
  "pitbull": "Pit Bulls are good boys and girls.",
  "german": "German Shepherds are good boys and girls."
})[breed]
dogSwitch("border") // "Border Collies are good boys and girls."

Pagine correlate

Pagine di esempio simili

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
English
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................