This commit is contained in:
14
05_react/uebungen/u02-03_mathfn-modul/assets/js/MathFn.js
Normal file
14
05_react/uebungen/u02-03_mathfn-modul/assets/js/MathFn.js
Normal file
@@ -0,0 +1,14 @@
|
||||
// Factory Function
|
||||
const MathFn = () => {
|
||||
// closures
|
||||
const add = (a, b) => a + b;
|
||||
const subtract = (a, b) => a - b;
|
||||
|
||||
const multiply = (a, b) => a * b;
|
||||
const divide = (a, b) => {
|
||||
return b === 0 ? NaN : a / b;
|
||||
};
|
||||
|
||||
export { add, subtract, multiply, divide };
|
||||
};
|
||||
export default MathFn;
|
||||
27
05_react/uebungen/u02-03_mathfn-modul/assets/js/main.js
Normal file
27
05_react/uebungen/u02-03_mathfn-modul/assets/js/main.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { add, subtract, divide, multiply } from './math';
|
||||
|
||||
import math from './math';
|
||||
|
||||
import MathFn from './MathFn';
|
||||
|
||||
const app = MathFn();
|
||||
|
||||
console.log(app.add(1, 2)); // => 3;
|
||||
console.log(app.subtract(3, 2)); //=> 1
|
||||
|
||||
console.log(app.multiply(2, 3)); // => 6
|
||||
console.log(app.divide(6, 3)); // => 2
|
||||
|
||||
console.log(add(1, 2)); // => 3;
|
||||
console.log(subtract(3, 2)); //=> 1
|
||||
|
||||
console.log(multiply(2, 3)); // => 6
|
||||
console.log(divide(6, 3)); // => 2
|
||||
|
||||
// -----------------------
|
||||
|
||||
console.log(math.add(1, 2)); // => 3;
|
||||
console.log(math.subtract(3, 2)); //=> 1
|
||||
|
||||
console.log(math.multiply(2, 3)); // => 6
|
||||
console.log(math.divide(6, 3)); // => 2
|
||||
37
05_react/uebungen/u02-03_mathfn-modul/assets/js/math.js
Normal file
37
05_react/uebungen/u02-03_mathfn-modul/assets/js/math.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// explizite Freigabe von Einheiten
|
||||
|
||||
// math.js
|
||||
export const add = (a, b) => a + b;
|
||||
export const subtract = (a, b) => a - b;
|
||||
|
||||
// Übung 2: Mathfunktionen als Modul
|
||||
|
||||
// Programmiere zwei weitere Module, die jeweils eine Funktion multiply und divide enthalten. Importiere die Funktionen in der main.js-Datei und führe sie aus.
|
||||
|
||||
export const multiply = (a, b) => a * b;
|
||||
export const divide = (a, b) => {
|
||||
return b === 0 ? NaN : a / b;
|
||||
};
|
||||
|
||||
// Übung 3: Exportieren Funktionen als Objekt
|
||||
|
||||
// Anstatt die Funktionen add, subtract, multiply und divide benannt zu exportieren, exportiere sie als Standard-Export in einem Objekt. Importiere das Objekt in der main.js-Datei und führe die Funktionen aus.
|
||||
|
||||
export default {
|
||||
add,
|
||||
subtract,
|
||||
multiply,
|
||||
divide,
|
||||
// add: add,
|
||||
// subtract: subtract,
|
||||
// multiply:multiply,
|
||||
// divide: divide,
|
||||
};
|
||||
|
||||
// Übung 2: Mathfunktionen als Modul
|
||||
|
||||
// Programmiere zwei weitere Module, die jeweils eine Funktion multiply und divide enthalten. Importiere die Funktionen in der main.js-Datei und führe sie aus.
|
||||
|
||||
// Übung 3: Exportieren Funktionen als Objekt
|
||||
|
||||
// Anstatt die Funktionen add, subtract, multiply und divide benannt zu exportieren, exportiere sie als Standard-Export in einem Objekt. Importiere das Objekt in der main.js-Datei und führe die Funktionen aus.
|
||||
Reference in New Issue
Block a user