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

Stringliterale in Hexcodes wandeln

/// Beim Speichern von Strings in Textdateien möchte man manchmal, dass Literale (Zeilenumbrüche, Tabs usw.)
/// nicht interpretiert werden, sondern als hexadezimale Codierungen gespeichert werden.
/// die statische Klasse wandelt alle ASCII-Steuerzeichen in einem String mit ASCIICode < 32 in die entsprechenden hexadezimalen Pendants um.
using System;

/// <summary>
/// freeware helper class for converting string formats
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary>
public static class StringConverter
{
    /// <summary>
    /// hex replace of literals
    /// </summary>
    /// <param name="path">a string</param>
    /// <returns>string with replaced literals</returns>
    public static string ReplaceLiterals(string inputstring)
    {
        string output = string.Empty;
        
        foreach (char t in inputstring)
        {
            int ascii = Convert.ToInt32(t);
            output += (ascii < 32) ? string.Format("&#x{0:X02};", ascii) : t.ToString();
        }

        return output;
    }
}

Beispiel:

string sInput = "Hallo" + Environment.NewLine + "Welt!"

// "Hallo Welt!"
string sOutPut = StringConverter.ReplaceLiterals(sInput));