โก JS Prototypes & this Binding
๐น 1. What are Prototypes?
Section titled โ๐น 1. What are Prototypes?โ- In Javascript:
- Every object has a hidden property called
[[Prototype]]which can be accessed using_proto_ - When you access a property on an object, JS looks:
-
- In the object itself.
-
- If not found, it delegates to its prototype chain.
-
- Eg:
const person = {name: "Sonam"}console.log(person.toString()) // [object object]
- โ
toStringis not defined in person, but JS finds it inObject.prototype.
- Every object has a hidden property called
๐น Prototype Inheritance
Section titled โ๐น Prototype Inheritanceโ function Person (name){ this.name = name }
Person.prototype.sayHello = function (){ return `Hello from : ${this.name}` }
const user = new Person('Sonam') console.log(user.sayHello())- โ
sayHellois not copied to each object โ itโs stored in prototype and shared.
๐ Interview One-liner
Section titled โ๐ Interview One-linerโโPrototypes allow objects to share methods without duplicating them in memory.โ
๐น 2. this Binding
Section titled โ๐น 2. this Bindingโthisdepends on how a function is called ,not where itโs defined.- Eg:
Case 1: Global Scope console.log(this) // In browser โ window, in Node โ {}
Case 2 : Inside a method const obj = { a: 10, getData: (){ console.log(this.a) } } obj.getData() // 10
โ
this refers to the object calling the method (obj).
Case 3: Lost this (Common Pitfall) const obj = { a: 10, getData: (){ console.log(this.a) } } const fn = obj.getData fn(); // undefined (in strict mode)
โ
Because now fn is just a function, not called on obj.
Case 4: this in Arrow Functions const obj = { name: "Sonam", greet: () => { console.log(this.name); } }; obj.greet(); // โ undefined
โ
Arrow functions do not bind their own this. They inherit this from the lexical scope (where they were created).โก function vs arrow function โ this Behavior
Section titled โโก function vs arrow function โ this Behaviorโ| Aspect | Regular Function (function) | Arrow Function (=>) |
|---|---|---|
Own this? | โ
Yes, this depends on how the function is called. | โ No, arrow functions do not bind this; they use this from the enclosing lexical scope. |
Default this in global scope (non-strict) | window (in browsers) | window (same as lexical scope) |
Default this in strict mode | undefined | Inherits from enclosing scope |
| Inside an object method | Refers to the object that called it | โ Refers to outer scope (usually window/undefined) |
| In a class method | Refers to the instance | โ Still lexical this (not the instance unless wrapped) |
Can use bind / call / apply? | โ Yes | โ No effect โ arrow functions ignore binding |
| Best use case | Object methods, class methods, dynamic this | Callbacks, promises, event listeners (when you want to inherit surrounding this) |
๐ง Interview One-liner
Section titled โ๐ง Interview One-linerโ- Regular function โ this is dynamic (depends on call).
- Arrow function โ this is lexical (depends on where defined).
๐ง Quick Interview Takeaways
Section titled โ๐ง Quick Interview Takeawaysโ- Prototype โ Object inheritance mechanism.
thisโ Value depends on call site, not definition.- Arrow functions โ no own
this, use lexical scope. bind/call/applyโ Explicitly controlthis.