From fabd565ebecc57c2fa9cefef748197d9b5d3a273 Mon Sep 17 00:00:00 2001 From: miqlangelo Date: Tue, 6 May 2025 13:54:41 +0200 Subject: [PATCH] arrow fn --- src/07-funktionsprogrammierung/arrow.ts | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/07-funktionsprogrammierung/arrow.ts diff --git a/src/07-funktionsprogrammierung/arrow.ts b/src/07-funktionsprogrammierung/arrow.ts new file mode 100644 index 0000000..e36f238 --- /dev/null +++ b/src/07-funktionsprogrammierung/arrow.ts @@ -0,0 +1,77 @@ +const add = (a: number, b: number) => { + return a + b; +}; + +console.log(add(2, 3)); + +const multiply = (a: number, b: number): number => a * b; +console.log(multiply(3, 4)); + +class Counter { + count = 0; + + // increment() { + // setTimeout(function () { + // console.log(this.count); + // }, 1000) + // } + increment() { + setTimeout(() => { + console.log(this.count); + }, 200); + } +} + +const counter = new Counter(); +counter.increment(); + +function normalFunction() { + console.log(arguments); +} + +normalFunction('a', 1, 3); + +const arrowFunction = (...args: any[]) => { + console.log(args); +}; + +arrowFunction('a', 33, true); + +class Person { + name = 'Alice'; + + sayHello = () => { + console.log(`Hallo, mein Name ist ${this.name}`); + }; +} + +const person = new Person(); +const greet = person.sayHello; +greet(); + +class Utils { + static normalFunction(value: string) { + return value.toUpperCase(); + } + + static arrowFunction = (value: string) => value.toLocaleLowerCase(); +} + +console.log(Utils.normalFunction('Hallo')); +console.log(Utils.arrowFunction('Hallo')); + +class NormalMethodExample { + method() { + return 'Ich bin eine normale Methode.'; + } +} + +class ArrowFunctionExample { + method = () => 'Ich bin eine Arrow Function'; +} + +const obj1 = new NormalMethodExample(); +const obj2 = new ArrowFunctionExample(); + +console.log(obj1.method === new NormalMethodExample().method); +console.log(obj2.method === new ArrowFunctionExample().method);