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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
