I am currently learning JavaScript using a book. An example explains the dual purpose of functions.
function Person(name) {
if (this instanceof Person) {
this.name = name
} else {
throw new Error("Nutze new")
}
}
let aPerson = new Person("Astrid");
// let aPerson = Person("Astrid"); // throws an error
let notAPerson = Person.apply(aPerson, ["Elmar"]);
let notAPerson2 = Person.call(aPerson, "Peter");
console.log(aPerson); // Object { name: "Peter" }
console.log(notAPerson); // undefined
console.log(notAPerson2); // undefined
I understand, that I can set a contex with the apply() or the call() method.
But I do not understand, why the variables notAPerson and notAPerson2 are undefined?
It would be great if someone could explain this to me.
returna value. It just modifies thethisobject.function doesNotReturnAnything() { console.log("this is the entire body") }when invoked does not produce a value.