JavaScript Closures, Callbacks and Callback Hell, Generator Func. ,Currying Function, Pure - Impure Function
1. JavaScript Closures.
Closures is the combination of a bundled function.
Closures gives you access to an outer function's scope from an inner function.
function init() {
var name = 'Mozilla';
function displayName() {
console.log('name --->', name);
// output : name ---> Mozilla
}
displayName();
}
init();
2. JavaScript Callbacks.
Function passed as an argument.
function test1(name, callback) {
console.log('name---->', name);
callback();
}
function test2() {
console.log('test2---->');
}
test1("Ankit", test2);
//output :
// name----> Ankit
// test2---->
3. JavaScript Callback Hell.
Every callback depends / waits for the previous callback.
setTimeout(() => {
console.log('test 1--->');
setTimeout(() => {
console.log('test 2--->');
}, 2000);
}, 5000);
// output :
// test 1--->
// test 2--->
4. JavaScript Generator Function.
A Generator Function is a special function in JavaScript that can pause its execution and resume later. It returns values one by one instead of returning all values at once.
It is created using the function* syntax and uses the yield keyword.
Useful for lazy loading, pagination, large data processing, and generating sequences.
function* genFunc() {
yield 1;
yield 2;
}
let abcFunc = genFunc();
console.log(abcFunc.next().value); // 1
console.log(abcFunc.next().value); // 2
console.log(abcFunc.next().value); // undefined
5. JavaScript Currying Function.
Currying is a technique in JavaScript where a function with multiple arguments is converted into a sequence of functions, each taking one argument at a time.
function sendEmail(to) { return function (from) { return function (body) { return to + ' - ' + from + ' - ' + body; } }}
let to = sendEmail('to@gmail.com');let from = to('from@gmail.com');console.log(from('Body data'));// output : to@gmail.com - from@gmail.com - Body data.
function sendEmail(to) { return function (from) { return function (body) { return to + ' - ' + from + ' - ' + body; } }}
console.log(sendEmail('to@gmail.com')('from@gmail.com')('Body data.'));// output : to@gmail.com - from@gmail.com - Body data.
function sendEmail(to) {
return function (from) {
return function (body) {
return to + ' - ' + from + ' - ' + body;
}
}
}
let to = sendEmail('to@gmail.com');
let from = to('from@gmail.com');
console.log(from('Body data'));
// output : to@gmail.com - from@gmail.com - Body data.
function sendEmail(to) {
return function (from) {
return function (body) {
return to + ' - ' + from + ' - ' + body;
}
}
}
console.log(sendEmail('to@gmail.com')('from@gmail.com')('Body data.'));
// output : to@gmail.com - from@gmail.com - Body data.
//using arrow functionconst sendemail = (to) => (from) => (body) => { return to + ' - ' + from + ' - ' + body};console.log('sendemail--->', sendemail('to@gmail.com')('from@gmail.com')('Body data.'));// output : sendemail---> to@gmail.com - from@gmail.com - Body data.
//using arrow function
const sendemail = (to) => (from) =>
(body) => { return to + ' - ' + from + ' - ' + body};
console.log('sendemail--->',
sendemail('to@gmail.com')('from@gmail.com')('Body data.'));
// output : sendemail---> to@gmail.com - from@gmail.com - Body data.
let add = (a) => (b) => (c) => a + b + c;console.log(add(1)(2)(3)); // output : 6
let add = (a) => (b) => (c) => a + b + c;
console.log(add(1)(2)(3)); // output : 6
6. Pure - Impure Function.
Pure Function : Always returns the same output for the same input. Like predictable output.
const pFunc = (a) => {
return a + 1;
};
console.log(pFunc(2)); // 3
console.log(pFunc(2)); // 3
Impure Function : Return different results for the same input.
let a = 2;
const impFunc = () => {
return a++;
};
console.log("impFunc--->", impFunc()); // 2
console.log("impFunc--->", impFunc()); // 3
console.log("impFunc--->", impFunc()); // 4

No comments:
Post a Comment