I have a constructor function defined as follows:
function Person(fname, lname) {
this.firstName = fname;
this.lastName = lname;
this.printName = function(fname, lname){
console.log("Name: " + this.firstName + " " + this.lastName);
}
}
Now, I can use the new keyword to create an object from my constructor function and call the "printName" method to print the created "Person" object firstName and lastName:
const p = new Person("John", "Doe");
p.printName(); // output: 'Name: John Doe'
I can also use the built-in javascript .call method with my constructor function to create a new object as follows:
Person.call({}, "John", "Doe");
Here's my question: How can I call the "printName" method in this case?
Person.calldoesn't create a new object. The object literal{}does. And it's only a plain object, not aPersoninstance, which is the reason why you should usenewinstead.Person.call({}, "John", "Doe").printName()? I dont get the question ...