"use strict";

// Scoping im Praxis
function calcAge(birthYear) {
  const age = new Date().getFullYear() - birthYear;
  function printAge() {
    let output = `${firstName}, du bist ${age} Jahre alt, geboren den ${birthYear}`;
    console.log(output);
    if (birthYear >= 1981 && birthYear <= 1996) {
      var millenial = true;

      const firstName = "Steven";

      output = "NEUE AUSGABE!";
      const str = `Ah, du bist tausendjähriger, ${firstName}`;
      console.log(str);
      function add(a, b) {
        return a + b;
      }
    }
    // console.log(str);
    console.log(millenial);
    // console.log(add(2, 3));
    console.log(output);
  }
  printAge();
  return age;
}
const firstName = "David";
calcAge(1986);
// console.log(age);
// printAge();

// Hoisting und TDZ im Praxis
// Variablen
console.log(me);
var me = "David";
let job = "Entwickler";
const year = 1986;
// Functions
console.log(addDecl(2, 3));
// console.log(addExpr(2, 3));
console.log(addArrow);
// console.log(addArrow(2, 3));
function addDecl(a, b) {
  return a + b;
}
const addExpr = function (a, b) {
  return a + b;
};
var addArrow = (a, b) => a + b;

console.log(undefined);
if (!numProducts) deleteShoppingCart();
var numProducts = 10;
function deleteShoppingCart() {
  console.log("Alle Produkte wurden entfernt!");
}
var x = 1;
let y = 2;
const z = 3;
console.log(x === window.x);
console.log(y === window.y);
console.log(z === window.z);

// Das Stichwort this im Praxis
console.log(this);
const calcAge = function (birthYear) {
  console.log(new Date().getFullYear() - birthYear);
  console.log(this);
};
calcAge(1991);
const calcAgeArrow = birthYear => {
  console.log(new Date().getFullYear() - birthYear);
  console.log(this);
};
calcAgeArrow(1980);
const david = {
  year: 1991,
  calcAge: function () {
    console.log(this);
    console.log(new Date().getFullYear() - this.year);
  },
};
david.calcAge();
const matilda = {
  year: 2017,
};
matilda.calcAge = david.calcAge;
matilda.calcAge();
const f = david.calcAge;
f();

// // Regulare Funktionen vs. Arrow Funktionen
// const david = {
//   firstName: "David",
//   year: 1986,
//   calcAge: function () {
//     // console.log(this);
//     console.log(new Date().getFullYear() - this.year);
//     const isMillenial = () => {
//       console.log(this);
//       console.log(this.year >= 1981 && this.year <= 1996);
//     };
//     isMillenial();
//   },
//   greet: () => {
//     console.log(this);
//     console.log(`Hello ${this.firstName}`);
//   },
// };
// david.greet();
// david.calcAge();

// // Argument keyword
// const addExpr = function (a, b) {
//   console.log(arguments);
//   return a + b;
// };
// addExpr(2, 5);
// addExpr(2, 5, 8, 12);
// var addArrow = (a, b) => {
//   console.log(arguments);
//   return a + b;
// };
// addArrow(2, 5, 8);

// // Objekte vs. Primitive
// let age = 30;
// let oldAge = age;
// age = 31;
// console.log(age);
// console.log(oldAge);
// const me = {
//   name: "David",
//   age: 36,
// };
// const friend = me;
// friend.age = 27;
// console.log("Friend:", friend);
// console.log("Me", me);

// // Primitives vs. Objects im Praxis
// // Primitive types
// let lastName = "Williams";
// let oldLastName = lastName;
// lastName = "Davis";
// console.log(lastName, oldLastName);

// // Reference Typen
// const joanne = {
//   firstName: "Joanne",
//   lastName: "Williams",
//   age: 27,
// };
// const marriedJoanne = joanne;
// marriedJoanne.lastName = "David";
// console.log("Before marriage:", joanne);
// console.log("After marriage: ", marriedJoanne);

// const joanne2 = {
//   firstName: "Joanne",
//   lastName: "Williams",
//   age: 27,
//   family: ["Alice", "Bob"],
// };
// const joanneCopy = Object.assign({}, joanne2);
// joanneCopy.lastName = "Davis";
// joanneCopy.family.push("Mary");
// joanneCopy.family.push("John");
// console.log("Before marriage:", joanne2);
// console.log("After marriage: ", joanneCopy);
