Methods and this — JS Technical Interview Questions & Answers
Functions stored on an object, and what this refers to.
Methods and this
- Methods and this (js object) is a JS topic. In plain words you use it for { name, marks } in a student record. Don’t start with a slogan — start with that picture.
- Smallest example: const { name } = student. Type it, run it, and say what you see. If you can do that from memory, you know Methods and this (js object).
- From the example next to this theory: Functions stored on an object, and what this refers to.
- Trap: copying nested objects by accident (shallow copy). Fix that before you talk about advanced JS.
- Viva: what is Methods and this (js object)? Then show const { name } = student. Then name the trap.
Technical Interview Tip: What is Methods and this (js object)? Show this: const { name } = student. Trap: copying nested objects by accident (shallow copy).
Code Examples for Methods and this
Methods and this — working example
const user = {
name: "Ada",
greet() {
return "Hi " + this.name;
},
};
console.log(user.greet());
function greet() {
return "Hi " + this.name;
}
console.log(greet.call({ name: "Lin" }));
console.log(greet.apply({ name: "Riya" }));
const bound = greet.bind({ name: "Sam" });
console.log(bound());Methods and this — this depends on how a function is called; call/apply/bind set it explicitly.
How to Explain Methods and this to an Interviewer
What is Methods and this (js object)?
Used for { name, marks } a student record Not just the heading
Give one small example of Methods and this (js object).
const { name } = student Functions stored on an object, and what this refers to
What mistake do beginners make with Methods and this (js object)?
copying nested objects by accident (shallow copy) Show the fix
Where do you use Methods and this (js object)?
a student record { name, marks }