Currying
function sum(x){
return function(y){
return (y!=undefined)?sum(x+y):x;
}
}
console.log(sum(10)(20)())
sum(10)(20)()
sum(30)()
returns 30
function outerFunction(outerVariable) {
// This inner function is a closure
function innerFunction(innerVariable) {
return outerVariable + innerVariable;
}
return innerFunction;
}
// Create a closure by calling outerFunction with an argument
const closure = outerFunction(10);
// Use the closure to access outerVariable
const result = closure(5); // result is 15
console.log(result);
pass data to sibling components in angular
parent has 2 childs
we need to pass from child to child
we use subject to communicate with multiple components
A subject is a special observable which acts as both observer and observable. They allow us to emit new values to the observable stream using the next method all the subscribers who subscribe to the subject will receive the same instance of subject and hence the same values
it can emit the data and subscribe as well.
In Angular, Subject and BehaviorSubject are two commonly used classes provided by the RxJS library to manage and propagate state and data within an application. They are often used for inter-component communication and managing shared application state. Here are some use cases for both Subject and BehaviorSubject:
-
Inter-Component Communication:
-
Subject: You can use a
Subjectto create an event bus or a central messaging system for components to communicate with each other. For example, you can notify multiple components when a user logs in or out by subscribing to the sameSubject.
-
Subject: You can use a
-
BehaviorSubject: A
BehaviorSubjectcan be used to share a piece of state among components. For example, you can use aBehaviorSubjectto manage the current user's authentication state so that any component that subscribes to it always gets the latest state.
-
State Management:
-
Subject: You can use a
Subjectto manage transient state that doesn't need an initial value. For instance, if you have an event-based system and you want to notify components of certain events, aSubjectcan be used.
-
Subject: You can use a
-
BehaviorSubject: Use a
BehaviorSubjectwhen you need to manage a state with an initial value that components should immediately receive upon subscription. For example, you can use aBehaviorSubjectto manage the state of a shopping cart with an initial set of items.
-
Form Data Sharing:
-
Subject: You can use a
Subjectto share form data among components. For example, when a form is filled out in one component, you can use aSubjectto notify other components about the form data changes.
-
Subject: You can use a
-
BehaviorSubject: If you want to ensure that new subscribers immediately receive the current form data when they subscribe, a
BehaviorSubjectcan be used. This is useful when components need access to the form data upon initialization.
-
Caching API Responses:
-
Subject: You can use a
Subjectto create a data cache for API responses. When one component fetches data from an API, it can notify aSubject, and other components can subscribe to it to access the cached data.
-
Subject: You can use a
-
BehaviorSubject: If you want to ensure that new subscribers immediately receive the most recent API response data, you can use a
BehaviorSubject. This is helpful when multiple components depend on the latest API data.
-
User Authentication State:
-
BehaviorSubject: Managing user authentication state is a common use case for
BehaviorSubject. You can use it to store the user's authentication status and propagate it to various parts of the application. When a component subscribes, it can immediately access the user's authentication state.
-
BehaviorSubject: Managing user authentication state is a common use case for
-
Navigation and Route Handling:
-
Subject: You can use a
Subjectto handle navigation and route changes. When a component needs to trigger a route change, it can notify aSubject, and other components (e.g., navigation components) can subscribe to it to react accordingly.
-
Subject: You can use a
These are just a few examples of how Subject and BehaviorSubject can be used in Angular. The choice between the two depends on your specific requirements, especially whether you need to provide an initial value to subscribers (which is the key difference between them).







Top comments (0)