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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
@bhaveshdaswani93
bhaveshdaswani93 / function_declaration.js
Created November 24, 2019 04:56
function declaration example
//es5 function expressiong
const funcAuth = function(email,password) {
//some logic
return 'You are login successfully';
}
//funciton expression with es6 arrow function
const funcSing = (name) => {
return `${name} sings: La ala laaa`;
}
@bhaveshdaswani93
bhaveshdaswani93 / first_class_function.js
Created November 24, 2019 05:10
In javascript function can be passed as argument or can be return value of function execution
//Function can be passed as argument
const sing = function(name) {
return `${name} is singing a song`;
}
const letPerson = function(name,fn) {
return fn(name);
}
letperson('Lorem',sing);
@bhaveshdaswani93
bhaveshdaswani93 / closure_example.js
Created November 24, 2019 05:55
This is an example to understand closure
function a() {
let grandFather = 'grandpa';
return funciton b() {
let father = 'father';
return function c() {
let son = 'son';
return `${grandFather} > ${father} > ${son}`;
}
}
}
@bhaveshdaswani93
bhaveshdaswani93 / closure_memory_efficient.js
Created November 24, 2019 08:19
In this example we will see how closure are memory efficient
function withOutClosure(index) {
const arr = new Array(8000).fill('Lorem ipsum olorem'); // some random heavy operation that generate data
return arr[index];
}
withOutClosure(543);
withOutClosure(6543);
withOutClosure(345);
withOutClosure(135);
@bhaveshdaswani93
bhaveshdaswani93 / closure_encapsulation.js
Created November 24, 2019 10:33
lets understand how closure help in encapsulation
function encapEg() {
let protectedData = 0; // this is the protected data that we only want to allow read access not direct change it
const accessTheData = () => protectedData; // this function will exported
const updateData = ()=> protectedData++; // we want to update the protected data internall
setInterval(updateData,1000); //this is the logic to update the data
return {
accessTheData // we return the function that only allow to access the protected data
}
}
@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",
@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 / 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 / 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}`;
}
}