Prototype chains, prototypal inheritance, and how the this keyword is bound in different contexts, each explained with a short example.
34 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a prototype in JavaScript?
- An object that serves as a template for other objects, containing properties and methods that can be inherited by instances.
- How do you access an object's internal prototype?
- Using Object.getPrototypeOf(obj) or the legacy __proto__ property. Modern code prefers Object.getPrototypeOf().
- What is the difference between obj.__proto__ and Function.prototype?
- __proto__ is an instance's link to its prototype, while Function.prototype is a property on a constructor function that becomes the __proto__ of new instances.
- What is the prototype chain?
- A series of linked prototypes where an object inherits properties from its prototype, which itself has a prototype, forming a chain that eventually ends at null.
- How does property lookup work in the prototype chain?
- JavaScript first checks the object itself; if not found, it checks the object's prototype, then the prototype's prototype, continuing until the property is found or null is reached.
- What does Object.getPrototypeOf(obj) return?
- The internal prototype of obj, equivalent to obj.__proto__ but using the standard recommended approach.
- What is a constructor function?
- A regular function intended to be called with the `new` keyword to create and initialize objects; by convention, constructor names are capitalized.
- What does the `new` keyword do?
- Creates a new empty object, sets the object's __proto__ to the constructor's prototype property, calls the constructor with `this` bound to the new object, and returns the object.
- Why should methods be defined on the prototype rather than in the constructor?
- Methods on the prototype are shared by all instances, saving memory; methods in the constructor create a new copy for each instance.
- How do you set up prototypal inheritance?
- Assign the parent's prototype to the child constructor's prototype using Object.create(), then reassign the constructor property to point back to the child.
- What is Object.create(proto)?
- Creates a new object with its internal prototype set to the proto argument, optionally adding properties via a descriptor map.
- What is `this` in JavaScript?
- A special keyword that refers to the object on which a method is being called; its value depends on how the function is invoked.
- What does `this` refer to in a regular function called without an object?
- In non-strict mode, `this` refers to the global object (window or global); in strict mode, `this` is undefined.
- What does `this` refer to when a method is called on an object?
- `this` refers to the object the method is called on (the object before the dot in object.method()).
- What does `this` refer to in a constructor called with `new`?
- `this` refers to the newly created object that the constructor is initializing.