#js #interview #deepClone #patterns
Analysis: Currying of the add function is achieved by recursively calling a function that continues to accept parameters.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #promise
Analysis: Use the Promise.all principle to synchronize the Promise state through the counter and result array.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #instanceof
The instanceof operator in JavaScript can be used to determine whether an object is an instance of another object. However, the instanceof operator has some limitations, such as:
🥫The instanceof operator can only determine objects directly connected to the prototype chain.
🌯The instanceof operator cannot detect objects with cyclic prototype chains.
Therefore, the above code provides a more general instanceof function that can determine the relationship between any two objects.
The implementation principle of this function is:
🍗The function instanceof receives two parameters: left and right.
🥞First, the code checks to see if the number of parameters is 2, and if not, throws an error.
🥚Next, the code checks whether the left operand left is null. If so, it returns false directly, because null cannot be an instance of any object.
🧀Then, the code checks whether the type of the left operand left is an object. If not, it returns false directly, because only objects can be instances of the constructor.
🥖Next, the code uses Object.getPrototypeOf() to obtain the prototype of the left operand left and assigns it to the variable proto.
🍞In a loop, the code continues to traverse proto’s prototype chain until proto is null.
🥐In the loop, the code checks whether the prototype of the right operand right is equal to the current proto. If they are equal, it means that the left operand left is an instance of the right operand right and returns true.
🍠If no matching prototype is found at the end of the loop, that is, proto is null, it means that the left operand left is not an instance of the right operand right, and false is returned.
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #records #tuples
⚠️ ECMAScript® 2024 (ES15)
✅ Article link
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
1🔥1
#js #promise
Create a cancellable promise P with a method cancel such that after we call the method P.cancel(), the promise cannot be resolved now. There should be a way to know that the promise was cancelled and not rejected.
✅ Article link
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #workers #debounce #srcset #requestAnimationFrame #IntersectionObserver
✅ Article link
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
❤1
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #generators #guide
When to Choose Which?
Use generators when:
— You need to manage complex control flows with precision.
— Memory efficiency is critical, and you want to generate values on demand.
— You’re implementing patterns like state machines or lazy iteration.
Use promises when:
— You’re handling simple asynchronous operations.
— You need to work with concurrent tasks.
— You prefer simpler, more straightforward error handling.
Best Practices for Using Generators:
1. Keep It Simple
Generators can add complexity to your code, so use them judiciously. If a task can be easily handled with promises or async/await, there’s no need to reach for generators.
2. Combine with Promises for Maximum Effect
Generators and promises are not mutually exclusive. In fact, they can complement each other beautifully. For instance, you can use a generator to structure your async flow and promises to handle the actual asynchronous operations.
3. Mind the Iteration
When using generators for iteration, always be mindful of when to stop. An infinite loop in a generator can be a real headache if not properly managed. Ensure you have clear exit conditions if your generator has the potential to run indefinitely.
4. Test Thoroughly
Given the unique execution flow of generators, thorough testing is crucial. Ensure that all possible execution paths are covered, including edge cases where the generator might yield unexpectedly or be terminated early.
5. Leverage TypeScript for Type Safety
Since you’re writing in TypeScript, make sure to define the types for your generator functions. This adds an extra layer of safety, helping you catch potential issues at compile time.
✅ Article link
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
❤2👍1
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1
#js #generators
Generators: Advantages
— Fine-Grained Control: Generators give you the ability to pause and resume function execution at will. This can be a game-changer in scenarios like lazy evaluation, custom iteration logic, or complex control flows.
— Memory Efficiency: Since generators produce values on demand, they are highly memory-efficient, making them perfect for handling large datasets or streams.
— Readable Asynchronous Code: While async/await has largely supplanted many asynchronous handling methods, generators still offer a unique combination of readability and control, making complex asynchronous flows easier to manage.
✅ Article link
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1