MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Proxy & Decorator Patterns0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
9.1Singleton Pattern & Module Pattern9.2Factory & Builder Patterns9.3Observer & Pub/Sub Pattern9.4Strategy & State Patterns9.5Proxy & Decorator Patterns
HomeTutorialsJavaScript for AdvancedProxy & Decorator Patterns
TypedArrays & ArrayBuffers

Proxy & Decorator Patterns

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Structural design patterns manage how objects and classes are composed into larger architectures. The Proxy Pattern and the Decorator Pattern add functionality to existing objects without modifying their underlying source code.


1. The Proxy Pattern

A Proxy acts as an intermediary or surrogate for another object, intercepting operations (property access, assignment, function invocation) to add access control, caching, or logging.

JavaScript provides native language-level support via the Proxy object:

1const user = {
2 id: 101,
3 username: 'alex_dev',
4 role: 'editor',
5 salary: 85000
6};
7 
8// Security Proxy: Protects sensitive salary property
9const secureUserProxy = new Proxy(user, {
10 get(target, prop, receiver) {
11 if (prop === 'salary') {
12 throw new Error('Access Denied: You do not have clearance to view salary.');
13 }
14 return Reflect.get(target, prop, receiver);
15 },
16 
17 set(target, prop, value, receiver) {
18 if (prop === 'role' && value === 'admin') {
19 throw new Error('Unauthorized: Cannot grant admin privileges directly.');
20 }
21 return Reflect.set(target, prop, value, receiver);
22 }
23});
24 
25console.log(secureUserProxy.username); // "alex_dev"
26// secureUserProxy.salary; // Error: Access Denied!
27// secureUserProxy.role = 'admin'; // Error: Unauthorized!

2. Virtual Proxy for Lazy Loading

A Virtual Proxy delays the instantiation of an expensive resource until it is actually accessed:

1class Heavy3DModel {
2 constructor(modelPath) {
3 console.log(`[Expensive] Loading 3D model meshes from: ${modelPath}`);
4 this.modelPath = modelPath;
5 }
6 render() { console.log(`Rendering 3D model ${this.modelPath}`); }
7}
8 
9class Lazy3DModelProxy {
10 constructor(modelPath) {
11 this.modelPath = modelPath;
12 this.realModel = null; // Instantiation deferred!
13 }
14 
15 render() {
16 if (!this.realModel) {
17 this.realModel = new Heavy3DModel(this.modelPath);
18 }
19 this.realModel.render();
20 }
21}
22 
23const proxyModel = new Lazy3DModelProxy('/assets/car.obj');
24// No heavy loading has occurred yet!
25console.log('App loaded.');
26 
27// Loading happens ONLY when render() is called:
28proxyModel.render();

3. The Decorator Pattern

The Decorator Pattern dynamically attaches additional responsibilities and behaviors to an object without altering its class definition.

1// Base Component
2class Coffee {
3 cost() { return 5; }
4 description() { return 'Simple Coffee'; }
5}
6 
7// Decorator 1: Milk
8function withMilk(coffee) {
9 const originalCost = coffee.cost();
10 const originalDesc = coffee.description();
11 return {
12 ...coffee,
13 cost: () => originalCost + 1.5,
14 description: () => `${originalDesc}, with Steamed Milk`
15 };
16}
17 
18// Decorator 2: Caramel
19function withCaramel(coffee) {
20 const originalCost = coffee.cost();
21 const originalDesc = coffee.description();
22 return {
23 ...coffee,
24 cost: () => originalCost + 2.0,
25 description: () => `${originalDesc}, with Caramel Drizzle`
26 };
27}
28 
29// Stacking decorators dynamically:
30let myOrder = new Coffee();
31myOrder = withMilk(myOrder);
32myOrder = withCaramel(myOrder);
33 
34console.log(myOrder.description()); // "Simple Coffee, with Steamed Milk, with Caramel Drizzle"
35console.log(`Total: $${myOrder.cost()}`); // Total: $8.5

4. Comparing Proxy vs. Decorator

FeatureProxy PatternDecorator Pattern
IntentControls, intercepts, and manages access to an objectDynamically adds new features and responsibilities
InterfaceTypically mirrors target interface identicallyEnhances or expands target interface
RelationshipOften instantiated internally or created as wrapperComposed and chained by the client

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary purpose of the Proxy Pattern?
Question 2
What native JavaScript object allows intercepting fundamental operations like property lookup and assignment?
Question 3
What is a "Virtual Proxy"?
Question 4
How does the Decorator Pattern differ from standard Class Inheritance?
Question 5
What standard JavaScript API should be used inside Proxy traps to perform default behavior on the target?
Next Lesson

TypedArrays & ArrayBuffers

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Strategy & State PatternsTypedArrays & ArrayBuffers

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext