Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 21 / 34

Objects & the prototype chain

01

Understand the idea

A reads successfully even though .keys does not list it. The value was inherited from a prototype.

A lookup route for shared behavior

Imagine giving many accounts the same helper . Copying that method into every account is unnecessary. A prototype lets those accounts share a place to find it, while each account keeps its own name and settings.

Own and inherited properties

An object can have its own properties and inherit others through its internal prototype. Inheritance does not copy those properties into every object. A method on a shared prototype can therefore serve many instances without storing a separate method on each one.

An own property lives directly on the object you are inspecting. An inherited property is found farther along its lookup route. Reading account.role can succeed even when Object.hasOwn(account, 'role') is false. Reading a property and asking who owns it are different questions.

Reading a property versus checking ownership
OperationQuestion it answersIncludes inherited properties?
object.keyWhat value does lookup find?Yes
Object.hasOwn(object, "key")Does this object itself own the property?No
"key" in objectDoes lookup find the property anywhere?Yes
Object.keys(object)Which enumerable string keys does this object own?No
Object.getPrototypeOf(object)What is the next object on the route?Returns the link, not the properties

Shadowing a shared property

The first match wins. Adding an own property with the same name shadows an inherited one; deleting that own property can reveal the inherited value again. Ordinary assignment usually creates or changes an own data property, though inherited setters and non-writable properties can alter that behavior.

The route ends at null

Object.hasOwn asks whether a property belongs directly to an object. The in operator also includes inherited properties. Object.getPrototypeOf inspects the next link. Object.keys lists own enumerable string keys, so it is not a complete view of everything property lookup can find.

Use the right inspection tool

A 's public prototype property is different from an object's internal prototype link. When used with new, an ordinary constructor supplies its prototype object as the 's next link. Avoid modifying built-in prototypes: it changes the lookup route for unrelated code across the application.

Object.getPrototypeOf lets you inspect the next link. The function property named prototype, used by constructor functions, is different from an individual object's internal prototype link. The names are easy to confuse, so ask whether you are looking at the object being used or the constructor that creates instances. Ordinary missing-property reads end with ; reading a property on null itself still throws.

02

Mental model

account

name

role

shared

role

Object.prototype

toString, hasOwnProperty

null

End of the chain

Check account for an own property named role.

01 / 03Follow [[Prototype]]

03

Live example

account first inherits reader. Assigning its own role later shadows that inherited value without changing shared.role.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Connect the prototype link

Create account with shared as its prototype so the initial role can be inherited.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

reader
false
editor

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Where does lookup stop?
    02Does Object.hasOwn include inherited properties?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson