[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());