Object Literal
Debounce
Design Pattern
Guard Statement
Tuple
Coupling
Tight coupling : In general, Tight coupling means the two classes often change together. In other words, if A knows more than it should about the way in which B was implemented, then A and B are tightly coupled.
Loose coupling : In simple words, loose coupling means they are mostly independent. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled. In order to over come from the problems of tight coupling between objects, spring framework uses dependency injection mechanism with the help of POJO/POJI model and through dependency injection its possible to achieve loose coupling.
Tuple
1 2 3 4 5 6 |
function getTuple() { return ["John", "Doe", 24]; } var [userName, userLastName, userAge] = getTuple(); console.log(`Name is: ${userName} ${userLastName} and his age is: ${userAge}`); |
Output Will Be
Debounce
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function debounce(fn, delay) { let timer; return (() => { clearTimeout(timer); timer = setTimeout(() => fn(), delay); })(); }; // usage function someFn() { console.log('Hi') }; debounce(someFn, 1000); |
Swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
typealias Debounce<T> = (_ : T) -> Void func debounce<T>(interval: Int, queue: DispatchQueue, action: @escaping Debounce<T>) -> Debounce<T> { var lastFireTime = DispatchTime.now() let dispatchDelay = DispatchTimeInterval.milliseconds(interval) return { param in lastFireTime = DispatchTime.now() let dispatchTime: DispatchTime = DispatchTime.now() + dispatchDelay queue.asyncAfter(deadline: dispatchTime) { let when: DispatchTime = lastFireTime + dispatchDelay let now = DispatchTime.now() if now.rawValue >= when.rawValue { action(param) } } } } let debouncedFunction = debounce(interval: 200, queue: DispatchQueue.main, action: { (identifier: String) in print("called: \(identifier)") }) // Call function debouncedFunction("1") |