programming:javascript:usage
This is an old revision of the document!
Table of Contents
Funktionen
Objekte
Ein Objekt gruppiert mehrere Werte zusammen:
const objektEins = { name: 'Hans', alter: 27 }; console.log(objektEins); console.log(objektEins.name); console.log(objektEins.alter); > {name: 'Hans', alter: 27} > Hans > 27
Neuer Wert in ein bestehendes Objekt hinzufügen:
objektEins.wohnort = 'Mainz'; console.log(objektEins); > {name: 'Hans', alter: 27, wohnort: 'Mainz'} > Mainz
Einfache bedingte Ausdrücke
Allgemein bei return:
predicate ?
consequent-expression :
alternative-expression
predicate: x >= 0
consequent-expression: x
alternative-expression: -x
function abs(x) { return x >= 0 ? x : - x; }
Mehrfach bedingte Ausdrücke
p1, p2, … pn: Prädikate
e1, e2, … en: Ausdrücke
final-alternative-expression: letzter Ausdruck fall alle anderen nicht zutreffen.
p1 ?
e1 :
p2 ?
e2 … :
pn ?
en :
final-alternative-expression
function abs(x) { return x > 0 ? x : x === 0 ? 0 : -x; }
zusammengesetzte Bedingungen
expression1 &&
expression2 → und
entspricht: expression1 ?
expression2 :
false
> function test_zahl(z){ return z>0 && z<11 ? z : console.log("falsch")} > test_zahl(2) 2 > test_zahl(22) falsch
> function test2_zahl(z){ return z>0 ? z<11 : console.log("falsch")} > test2_zahl(2) true > test2_zahl(22) false > test2_zahl(-2) falsch
expression1 ||
expression2 → oder
entspricht: expression1 ?
true
:
expression2
!
expression → nicht (negation)
programming/javascript/usage.1701857057.txt.gz · Last modified: 2023/12/06 11:04 by ms