Introduction
This article will try to cover all the JavaScript prerequisites that we must be aware of before we start learning ReactJS.
Please refer to my previous article, i.e., Part 1, to learn a few more concepts.
This article will cover the following.
- Implementation - Const Functions
- Implementation - Pure functions
- Implementation - Mutability in List
- Implementation – Immutability in List
- Implementation - Named parameters
- Implementation - Call, apply and bind
- Implementation - SetTimeOut () using bind
Implementation- Const Functions
These are the functions where objects can’t be changed. Here, I will show you an example of the same.
Examples
- const add = (x, y) => x + y;
- console.log(add(1, 2)); // 3
- add = (x, y) => x - y; // Uncaught TypeError: Assignment to constant variable.
Implementation- Pure and Impure functions
- Anything under {} is an object.
- Pure Functions- It should have arguments, create a new copy of the object, and return the object
- Impure Functions- If any function can’t follow any of the above conditions, then it's an impure function.
Examples
IMPURE
- var employee = {
- Name: 'Gourav',
- Id: 123
- }
- function impure() {
- employee.Name = "Rahul"
- employee.Id = 222
- }
- impure()
- console.log(employee);
Output
- {Name: "Rahul", Id: 222}
PURE
- var employee = {
- Name: 'Gourav',
- Id: 123
- }
- var purefunction = emp => ({
- ...emp,
- Name: 'RahulPure',
- Id: 12111
- })
- console.log(purefunction(employee));
Output
- Name: "RahulPure", Id: 12111}
Implementation- Mutability in List
Here, we will see how we see mutability in a list, i.e., if there is a change in the list passed in the method, then it would change the base list object as well.
Example
- let list = [{
- title: 'A'
- }, {
- title: 'B'
- }]
- var impurefunction = function(value, titles) {
- titles.push({
- title: value
- })
- return titles
- }
- console.log(impurefunction('c', list));
- console.log(list);
Output
- 0: {title: "A"}
- 1: {title: "B"}
- 2: {title: "c"}
- 0: {title: "A"}
- 1: {title: "B"}
- 2: {title: "c"}
Implementation- Immutability in List
Here, we would see how we can achieve immutability in a list, i.e., if there is a change in the list passed in the method, then it would not change the base list object.
Example
- let list = [{
- title: 'A'
- }, {
- title: 'B'
- }]
- var purefunction = (value, listObj) => (listObj.concat({
- title: value
- }))
- console.log(purefunction('c', list));
- console.log(list);
Output
- 0: {title: "A"}
- 1: {title: "B"}
- 2: {title: "c"}
- 0: {title: "A"}
- 1: {title: "B"}

Join the conversation! Your thoughts help the community grow.