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