FlashKeepers

JavaScript · JavaScript · College and University

JavaScript Prototypes and the this Keyword

Prototype chains, prototypal inheritance, and how the this keyword is bound in different contexts, each explained with a short example.

About this set

34 basic cards for JavaScript JavaScript. It opens with "What is a prototype in JavaScript?" and "How do you access an object's internal prototype?". More College and University decks are listed below.

34 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What is a prototype in JavaScript?
Show answerAn 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?
Show answerUsing Object.getPrototypeOf(obj) or the legacy __proto__ property. Modern code prefers Object.getPrototypeOf().
What is the difference between obj.__proto__ and Function.prototype?
Show answer__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?
Show answerA 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?
Show answerJavaScript 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?
Show answerThe internal prototype of obj, equivalent to obj.__proto__ but using the standard recommended approach.
What is a constructor function?
Show answerA 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?
Show answerCreates 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?
Show answerMethods 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?
Show answerAssign 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)?
Show answerCreates a new object with its internal prototype set to the proto argument, optionally adding properties via a descriptor map.
What is `this` in JavaScript?
Show answerA 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?
Show answerIn 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?
Show answer`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`?
Show answer`this` refers to the newly created object that the constructor is initializing.

19 more cards in the app

Study this set free