TypeScript: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(→Links) |
|||
Zeile 6: | Zeile 6: | ||
= Konstanten, Variablen = | = Konstanten, Variablen = | ||
* 'var'-Variablen sind global, 'let'-Variablen sind lokal (blockgebunden). | * 'var'-Variablen sind global, 'let'-Variablen sind lokal (blockgebunden). | ||
< | <syntaxhighlight lang="ts"> | ||
const E_DISK: string = 'disk error'; | const E_DISK: string = 'disk error'; | ||
var globalLastMessage: string = 'Error'; | var globalLastMessage: string = 'Error'; | ||
Zeile 17: | Zeile 17: | ||
const [width, height] = [1, 2]; | const [width, height] = [1, 2]; | ||
const [width, ...rest] = [1, 2, 3, 4]; // rest hat den Wert [2, 3, 4] | const [width, ...rest] = [1, 2, 3, 4]; // rest hat den Wert [2, 3, 4] | ||
</ | </syntaxhighlight> | ||
= Typen = | = Typen = | ||
== String == | == String == | ||
< | <syntaxhighlight lang="ts"> | ||
let x ='Hi'; | let x ='Hi'; | ||
x = `2 lines: | x = `2 lines: | ||
Say: ${x} | Say: ${x} | ||
`; | `; | ||
</ | </syntaxhighlight> | ||
== Number == | == Number == | ||
== Any == | == Any == | ||
Zeile 36: | Zeile 36: | ||
= Klassen = | = Klassen = | ||
* Nur ein Konstruktor | * Nur ein Konstruktor | ||
< | <syntaxhighlight lang="ts"> | ||
class User { | class User { | ||
publicItems: string; | publicItems: string; | ||
Zeile 58: | Zeile 58: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Interfaces == | == Interfaces == | ||
< | <syntaxhighlight lang="ts"> | ||
interface Contact { | interface Contact { | ||
firstname: string; | firstname: string; | ||
Zeile 73: | Zeile 73: | ||
lastname = 'Smith'; | lastname = 'Smith'; | ||
} | } | ||
</ | </syntaxhighlight> | ||
= Operatoren = | = Operatoren = | ||
== spread-Operator == | == spread-Operator == | ||
< | <syntaxhighlight lang="ts"> | ||
let list1: number[] = [1, 2, 3]; | let list1: number[] = [1, 2, 3]; | ||
let list2: number[] = [...list, 4, 5, ..list]; | let list2: number[] = [...list, 4, 5, ..list]; | ||
myFunc(1, 2, 3); | myFunc(1, 2, 3); | ||
myFunc(...list1); | myFunc(...list1); | ||
</ | </syntaxhighlight> |
Version vom 14. Dezember 2021, 07:51 Uhr
Links
Konstanten, Variablen
- 'var'-Variablen sind global, 'let'-Variablen sind lokal (blockgebunden).
const E_DISK: string = 'disk error';
var globalLastMessage: string = 'Error';
for (let i=0; i < 4; i++){}
const myArray = [ 0, 'string', { firstname: 'Joe' } ];
const onlyNumbers: number[] = [ 1, 2, 3];
let arg: any = 5; ... arg = 'string';
let width, height: number;
let rest: number[];
const [width, height] = [1, 2];
const [width, ...rest] = [1, 2, 3, 4]; // rest hat den Wert [2, 3, 4]
Typen
String
let x ='Hi';
x = `2 lines:
Say: ${x}
`;
Number
Any
Enums
typedef Status = 'active' | 'inactive';
Klassen
- Nur ein Konstruktor
class User {
publicItems: string;
private privateMember: string = 'secret';
private privateNumber = 25; // impliziter Typ.
constructor(items: string){
this.privateItems = items;
}
append(item: string): string { this.publicItems += ';' + item; return this.publicItems; }
// getter + setter:
get currentNumber(): number { return this.privateNumber; }
set number(value: number): void { this.privateNumber = value; }
add(inc: number) => return privateNumber + number;
printAll(...args): void {
print(args[0] + ': ' + args);
}
}
class SuperUser extends User {
constructor(items: string){
super(items);
}
}
Interfaces
interface Contact {
firstname: string;
lastname: string;
}
const contact: Contact = {
firstname: 'Joe',
lastname: 'Doo'
}
class Address implements Contact {
firstname = 'Eve';
lastname = 'Smith';
}
Operatoren
spread-Operator
let list1: number[] = [1, 2, 3];
let list2: number[] = [...list, 4, 5, ..list];
myFunc(1, 2, 3);
myFunc(...list1);