This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mutateObj = { | |
first_name:'lorem', | |
last_name:'ipsum' | |
}; | |
let immutateObj = { | |
first_name:'irum', | |
last_name:'egestas' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Imperative: what to do and how to do | |
//Declarative: what to do and what should be done | |
// Task i want to loop through 1 to 5 and print in the console let see how we can do the code impertively as well as declaratively | |
//Imerative | |
for(i=1;i<=5;i++) { | |
console.log(i); | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//here i will try to explain a key concept idempotence with example | |
function notIdempotenceFn(num) { | |
return Math.random(num); | |
} | |
notIdempotenceFn(5); | |
notIdempotenceFn(5); | |
// the output of above two function will be differeht however passing the same input so the function is not idempotent | |
function idempotentFn(num) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = (num1,num2) => { | |
return num1+num2; | |
} | |
let b = (num) => { | |
return num*2; | |
} | |
console.log(b(a(3,4))) //output will be 14 | |
// here i can replace a(3,4) expression with value 7 value and this will not effect to the result of the program because its return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addTwoNumber(num1,num2) { | |
return num1 + num2; | |
} | |
addTwoNmber(3,4); // will return 7 no matter how many times is called | |
function multiplyWithCurrentTime(num) { | |
return num * new Date().getTime(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In this example i want to clear out the difference between function with side effect and a function without side effect | |
let arr = [1,2,3]; | |
function removeLastItem(input) { | |
input.pop(); | |
} | |
removeLastItem(arr); | |
console.log(arr); // output [1,2] (this function changes the orignal variable from [1,2,3] -> [1,2]) | |
// above execution has side effect as it mutate arr which belong to the outside world. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
{ | |
constructor(first_name,last_name) { | |
this.first_name = first_name; | |
this.last_name = last_name; | |
} | |
getFullName() { | |
return `Full Name is: ${this.first_name} ${this.last_name}`; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let person = { | |
first_name:'lorem', | |
last_name:'ipsum', | |
getFullName(){ | |
return `Fullname is ${this.first_name} ${this.last_name}`; | |
} | |
} | |
let personA = Object.create(person); | |
personA.first_name = 'ullamcorper'; //overwrite the person first_name property |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Person = function(first_name,last_name) { | |
this.first_name = first_name; | |
this.last_name = last_name; | |
} | |
Person.prototype.getFullName = function() { | |
return `Full Name is: ${this.first_name} ${this.last_name}`; | |
} | |
let personA = new Person('lorem','ipsum'); | |
let personB = new Person('felis','ullamcorper'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let personA = { | |
first_name: "lorem", | |
last_name: "ipsum", | |
getFullName() { | |
return `Full Name is: ${this.first_name} ${this.last_name}`; | |
} | |
}; | |
let personB = { | |
first_name: "rutrum", |