Skip to content

Instantly share code, notes, and snippets.

View bhaveshdaswani93's full-sized avatar
👋
Building Awesome things with Laravel React Vue

Bhavesh Daswani bhaveshdaswani93

👋
Building Awesome things with Laravel React Vue
View GitHub Profile
@bhaveshdaswani93
bhaveshdaswani93 / immutability.js
Created December 27, 2019 04:02
In this example i will try to explain immutability
let mutateObj = {
first_name:'lorem',
last_name:'ipsum'
};
let immutateObj = {
first_name:'irum',
last_name:'egestas'
};
@bhaveshdaswani93
bhaveshdaswani93 / imperative_vs_declarative.js
Created December 26, 2019 03:36
Imperative vs Declarative with example. Here i want you to understand what exactly these terms are
//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);
}
/*
@bhaveshdaswani93
bhaveshdaswani93 / idempotence.js
Last active December 30, 2019 03:40
Let's understand a key term idempotence in functional programming
//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) {
@bhaveshdaswani93
bhaveshdaswani93 / referential_transparency.js
Created December 24, 2019 03:36
Understand Referential transparency with example
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
@bhaveshdaswani93
bhaveshdaswani93 / pure_function_same_input_same_output.js
Created December 22, 2019 12:37
Pure function should qualify with if there is same input the output should be same no matter how many times it is called
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();
}
@bhaveshdaswani93
bhaveshdaswani93 / pure_function_no_side_effect.js
Created December 22, 2019 12:14
Pure function that does not generate side effect
// 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.
@bhaveshdaswani93
bhaveshdaswani93 / oop_classes.js
Created December 22, 2019 06:00
Understanding classes in javascript
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}`;
}
}
@bhaveshdaswani93
bhaveshdaswani93 / object_create.js
Created December 22, 2019 05:41
This shows an example of how Object.create is use to create prototype
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
@bhaveshdaswani93
bhaveshdaswani93 / constructorFunction.js
Created December 22, 2019 04:43
This gist show an example of constructor function.
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');
@bhaveshdaswani93
bhaveshdaswani93 / factory_function.js
Created December 8, 2019 12:21
This gist try to explain factory function
let personA = {
first_name: "lorem",
last_name: "ipsum",
getFullName() {
return `Full Name is: ${this.first_name} ${this.last_name}`;
}
};
let personB = {
first_name: "rutrum",