Skip to content

Instantly share code, notes, and snippets.

@vipul-zambare006
Last active April 21, 2021 15:04
Show Gist options
  • Save vipul-zambare006/dcc739c38f2bb41881df8c58ac5db1d6 to your computer and use it in GitHub Desktop.
Save vipul-zambare006/dcc739c38f2bb41881df8c58ac5db1d6 to your computer and use it in GitHub Desktop.
JavaScript concepts
///HOISTING IS JAVASCRIPT:
/*
var a = 10;
In above line of code there are two things:
1. declaring variable that is: var a;
2. defining value of variable: a = 10;
Hoisting means variable declarations gets hoisted at the top.
check below example
a = 10;
console.log(a) //This will print 10
var a;
In above example we are defining value of variable before declaration but still console will print the value 10.
Because variable declaration gets hoisted at the top.
Another example:
console.log(sum(2,2))// prints '4'
function sum(a,b){ return a+b;}
This example also we first try to call sum before its declarations.
this is possible in javasceript because of Hoisting
Named functions gets hoisted but there is drawback. Hoisting takes space in browser memory not a lot but yes.
If you have so many functions hoisted then it will hit your performance.
Anonymous functions DOES NOT gets hoisted.
example:
function() { return "Hello"};
but you can not call Anonymous.
so you need to assign it to some variable.
var a = function {return "Hello";}
console.log(a());
This code will work. but what if you first try to call function.
console.log(a()); //calling anonymous function before declaration will not work. it will say 'a' is undefined.
var a = function {return "Hello";}
HOISTING in var and let
console.log(a) //prints 'undefined'
var a;
===========
console.log(a); //prints 'reference error: a is not defined'
let a;
============
var gets hoisted at the top and javascript assigns 'undefined' value to var.
let DOES NOT gets hoisted so it gives reference error.
*/
const addThree = (number) =>{
return addThree(number);
return number + 3;
}
addThree(5)(4); //8
// Three Number Sum
// Write a function that takes in a non-empty array of distinct integers and an
// integer representing a target sum. The function should find all triplets in
// the array that sum up to the target sum and return a two-dimensional array of
// all these triplets. The numbers in each triplet should be ordered in ascending
// order, and the triplets themselves should be ordered in ascending order with
// respect to the numbers they hold.
// array = [12, 3, 1, 2, -6, 5, -8, 6]
// targetSum = 0
// Le Vinh Phuc11:23 AM
// Same output
// [[-8, 2, 6], [-8, 3, 5], [-6, 1, 5]]
function ThreeSUM(arr, targetSum){
let sorted = arr.sort((a-b)= a-b);
let left = arr[0];
let right = arr[arr.length-1];
//-8,12 //0
//-6 6 //0
//1 5 -6
//move left pointer
//move right pointer
// let numberToSearch = target - sum of left and right
// find this number to search in array
// if its found then add it to result array
let numberToFind = target - (arr[left] + arr[right]);
arr.filter((i) => arr[])
}
//Currying in JavaScript
//Lodash library uses currying.
const sum = (x) => {
return (y) =>{
return (z) =>{
return x + y+ z;
}
}
}
sum(3)(4)(3);
const addToFive = sum(5)(5);
addToFive(4);
//Call, Apply and Bind:
//Call and Apply are almost same things they helps you to call function on any object.
//1. When using call you have to pass comma separated arguments
//2. When using apply you need to pass arguments as an array
//In below example getFullName method is bind to personObj.
function getFullName (lastName, age){
return `${this.firstName} ${lastName} ,Age: ${age}`;
}
const personObj = {
firstName: 'Vipul'
}
const result = getFullName.call(personObj,'Zambare',28);
console.log(result);
const applyResult = getFullName.apply(personObj,['Zambare', 28]);
console.log(applyResult);
//BIND: bind() method creates and returns a copy of the function func.
const carObject = {
number: 'CS800',
make:'BMW',
getInfo: function (info){
return `${this.number} - ${this.make}: ${info}`;
}
}
const printInfo = carObject.getInfo;
printInfo(); //This line will print undefined as in global scope you do not have number and make.
//We are binding printInfo and passing context of ``this`` as first argument and ``method parameters`` as second argument.
const boundedGetInfo = printInfo.bind(carObject, 'This is cool car');
boundedGetInfo(); //output: "CS800 - BMW: This is cool car"
// Conclusion
// call() and apply() executes the function immediately, whereas bind() returns a new function.
//the object/value on which the function executes depends on the this value defined by the context.
//DEBOUNCING AND THROTLLING
//https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf
//
===> JAVASCRIPT PROMISES:
const promise1 = new Promise((resolve, reject) => {
Geolocation.getCurrentPosition((data) => resolve(data.coords));
});
const promise2 = GetLocationsData("@locations_data");
const promise3 = new Promise((resolve, reject) => {
DeviceEventEmitter.addListener("headingUpdated", (headingAngle) => {
resolve(headingAngle);
});
});
Promise.all([
promise1,
promise2,
promise3,
]).then(([deviceLocCoords, currentLocations, headingAngle]) => {
this.setState({
lat: deviceLocCoords.latitude,
lng: deviceLocCoords.longitude,
heading: headingAngle,
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment