Understanding How JavaScript Prototype Chain Handles Property Access and Overwrites

This article explains how JavaScript's prototype chain determines whether property accesses create new child‑scope members, update inherited values, or shadow parent properties, illustrating each case with code examples and diagrams.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Understanding How JavaScript Prototype Chain Handles Property Access and Overwrites

Assume the parent scope parentScope defines the members aString, aNumber, anArray, and anObject. The child scope childScope inherits from parentScope, forming the prototype chain shown below.

When childScope accesses a property defined in parentScope, JavaScript first searches childScope. If the property is absent, it follows the prototype chain upward until it reaches the root scope.

Executing childScope.aString = 'child string' creates a new aString property on childScope without consulting the prototype chain, thereby shadowing the same‑named property in parentScope.

Running childScope.anArray[1] = '22' and childScope.anObject.key1 = 'child prop1' triggers prototype lookup because anArray and anObject are not present on childScope. The lookup finds them in parentScope and updates their internal values; no new properties or objects are created on childScope.

These operations modify the contents of existing properties rather than assigning new top‑level properties.

When assigning whole objects, as in childScope.anArray = [100, 555] and childScope.anObject = { name: 'Mark', country: 'USA' }, the prototype chain is not consulted. Two new properties are added to childScope, each shadowing the corresponding property in parentScope.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptInheritancescopeprototype chainproperty overriding
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.