[JavaScript] Klassendeklaration über “function”

Variante 1 (public)

// https://www.phpied.com/3-ways-to-define-a-javascript-class/
// https://developer.mozilla.org/de/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

// Klasse Person über "function" abbilden
function Person(vorname, nachname, alter) {
  this.Vorname = vorname;
  this.Nachname = nachname;
  this.Alter = alter;
  this.getText = function(type){
    var text = "";
     
    switch (type) {
      case "1":
        text = this.Vorname;
        break;
      case "2":
        text = this.Nachname;
        break;
      case "3":
        text = this.Alter;
        break;
      default:
        text = "-";
        break;
    }
     
    return text;
  };
}

var p = new Person("Udo", "Mustermann", 30);

// Datenausgabe
console.log(p.getText("1"));

Variante 2 (private und public)

function User() {

  // private
  a = 1;
  
  function private() {
    return a;
  }

  // public
  this.b = 2;

  this.public = function() {
    return this.b;
  };
}

let u = new User();
// error
//u.private();
// undefined
console.log(u.a);
// 2
console.log(u.b);
// 2
console.log(u.public());

[JavaScript] Einfache Objektdefinition

einfache Objektdefinition mit Literalen

// https://www.w3schools.com/js/js_objects.asp
// https://www.phpied.com/3-ways-to-define-a-javascript-class/

let person = {
  Vorname: "Horst",
  Nachname: "Mustermann",
  Alter: 65,
  getText: function(type){
    let text = "";
     
    switch (type) {
      case "1":
        text = this.Vorname;
        break;
      case "2":
        text = this.Nachname;
        break;
      case "3":
        text = this.Alter;
        break;
      default:
        text = "-";
        break;
    }
     
    return text;
  }
};

// Horst -> Klaus
person["Vorname"] = "Klaus";

// Datenausgabe
console.log(person.Vorname);
console.log(person["Vorname"]);
console.log(person.getText("1"));
console.log(person.getText("2"));
console.log(person.getText("3"));

einfache Objektdefinition mit new Object

let person = new Object();

person.Vorname = "Horst";
person.Nachname = "Mustermann";
person.Alter = 65;
person.getText = function(type){
  let text = "";
   
  switch (type) {
    case "1":
      text = this.Vorname;
      break;
    case "2":
      text = this.Nachname;
      break;
    case "3":
      text = this.Alter;
      break;
    default:
      text = "-";
      break;
  }
   
  return text;
};
 
// Datenausgabe
console.log(person.Vorname);
console.log(person["Vorname"]);
console.log(person.getText("1"));
console.log(person.getText("2"));
console.log(person.getText("3"));

alternative Objektdefinition mit new Object + Literalen

let person = new Object({
  Vorname : "Horst",
  Nachname : "Mustermann",
  Alter : 65,
  getText : function(type){
      let text = "";
       
      switch (type) {
        case "1":
          text = this.Vorname;
          break;
        case "2":
          text = this.Nachname;
          break;
        case "3":
          text = this.Alter;
          break;
        default:
          text = "-";
          break;
      }
       
      return text;
  }
});

// Datenausgabe
console.log(person.Vorname);
console.log(person["Vorname"]);
console.log(person.getText("1"));
console.log(person.getText("2"));
console.log(person.getText("3"));

[JavaScript] Arrays definieren

Eindimensionale Arrays

// https://www.w3schools.com/jsref/jsref_obj_array.asp
// https://www.w3schools.com/js/js_array_methods.asp

// leeres array
let arr_e = [];
console.log(arr_e.length);

// String array
let arr_s = ["a", "b", "c"];
for (let s in arr_s)
{
    console.log(arr_s[s]);
};

// Float array
let arr_f = [1.1, 2.1, 0.3];
for (let f in arr_f)
{
  console.log(arr_f[f]);
}

// Int array
let arr_i = [1, 2, 3];
for (let i in arr_i)
{
    console.log(arr_i[i]);
}

// new Array()
let arr_col = new Array('rot', 'grün', 'blau');

for (let c in arr_col)
{
    console.log(arr_col[c]);
}

// new Array()
let arr = new Array(2);

arr[0] = 1;
arr[1] = "Ede";

for (let c in arr)
{
    console.log(arr[c]);
}

Mehrdimensionale Arrays

// Array in Literalschreibweise
let person = [
  {
    Name: "Udo",
    Alter: 30,
    Ort: "Dresden",
    Handys: {
      Samsung: "S9",
      Huawei: "P20"
    }
  },
  {
    Name: "Heinz",
    Alter: 40,
    Ort: "Berlin",
    Handys: {
      Samsung: "S8",
      Huawei: "P8 Lite"
    }
  },
  {
    Name: "Ede",
    Alter: 50,
    Ort: "Bonn",
    Handys: {
      Samsung: "S7",
      Huawei: "Nova"
    }
  }
];

person.forEach(function(p, a)
{
    console.log("Person: " + a);
                
    for (let attr in p)
    {
        if (attr != "Handys")
        {
          console.log("  " + attr + ": " + p[attr]);
        }
        else
        {
          console.log("    " + p[attr].Samsung );
          console.log("    " + p[attr].Huawei );
        }
    }
});