🚨 JavaScript Interview Question (Advanced 🔥) What will be the output? 🤔 function Person(name) { this.name = name; } Person.prototype.getName = function () { return this.name; }; const p1 = new Person("Suman"); const p2 = { name: "Rahul", }; console.log(p1.getName()); console.log(p1.getName.call(p2)); Looks simple… but there’s a twist 👀 👉 What will be the output? 👉 Why does the second call behave differently? Bonus: What concept is being tested here? 🔥 #JavaScript #FrontendInterview #Prototypes #ThisKeyword #WebDevelopment
console.log(p1.getName())=> Suman => p1 console.log(p1.getName.call(p2))=> Rahul => p2 call() method is used invoke a function with a specific this value
Suman : getName is called on p1 so value of this is p1 Rahul: call invokes a function with this value so value of this is p2