Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Advanced JavaScript
  • Table Of Contents Toc
  • Feedback & Rating feedback
Advanced JavaScript

Advanced JavaScript

By : Zachary Shute
3.5 (4)
close
close
Advanced JavaScript

Advanced JavaScript

3.5 (4)
By: Zachary Shute

Overview of this book

If you are looking for a programming language to develop flexible and efficient applications, JavaScript is an obvious choice. Advanced JavaScript is a hands-on guide that takes you through JavaScript and its many features, one step at a time. You'll begin by learning how to use the new JavaScript syntax in ES6, and then work through the many other features that modern JavaScript has to offer. As you progress through the chapters, you’ll use asynchronous programming with callbacks and promises, handle browser events, and perform Document Object Model (DOM) manipulation. You'll also explore various methods of testing JavaScript projects. In the concluding chapters, you'll discover functional programming and learn to use it to build your apps. With this book as your guide, you'll also be able to develop APIs using Node.js and Express, create front-ends using React/Redux, and build mobile apps using React/Expo. By the end of Advanced JavaScript, you will have explored the features and benefits of JavaScript to build small applications.
Table of Contents (9 chapters)
close
close

Chapter 5: Functional Programming


Activity 5 – Recursive Immutability

You are building an application in JavaScript and your team has been told that it cannot use any third-party libraries for security reasons. You must now use Functional Programming (FP) principles for this application and you need an algorithm to create immutable objects and arrays. Create a recursive function that enforces the immutability of objects and arrays at all levels of nesting with Object.freeze(). For simplicity, you can assume that there are no null or classes nested in the objects. Write your function in activities/activity5/activity-test.js. This file contains code to test your implementation.

To demonstrate forcing immutability in objects, follow these steps:

  1. Open the activity test file at activities/activity5/activity-test.js.

  2. Create a function called immutable that takes in a single argument, data.

  3. Check to see if data is not of type object. If it is not, then return.

  4. Freeze the data object. You don't need to freeze non-objects.

  5. Loop through the object values with object.values and a forEach() loop. Recursively call the immutable function for each.

  6. Run the code contained in the test file. If any tests fail, fix the bugs and rerun the test

Code:

activity-solution.js
function immutable( data ) {
 if ( typeof data !== 'object' ) {
   return;
 }
 Object.freeze( data );
 Object.values( data ).forEach( immutable );
}

Snippet 5.11: Recursive immutability

Take a look at the following output screenshot below:

Figure 5.7 : Passed Tests output display

Outcome:

You have successfully demonstrated forcing immutability in objects.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Advanced JavaScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon