Angular ๐Ÿ‡บ๐Ÿ‡ฆ - practical notes
1.63K subscribers
1.6K photos
1 file
532 links
Angular - practical notes

This group is for posting practical notes for Angular developers. Mostly all posts are for quick implementation https://t.me/angular_practical_notes (Commenting on posts only in ENG and UA langs here). Welcome!
Download Telegram
๐Ÿšณ Interview Questions: Promise object

#js #interview #promise

Problem: Implement a simple version of Promise:

The Promise object is an asynchronous programming solution for handling asynchronous events. Promise objects can represent the status of an asynchronous operation, including:

- pending
- fulfilled
- rejected

Analysis:

๐ŸŒถThe MyPromise class is a custom Promise class whose constructor accepts an executor function as a parameter.
๐ŸŒฝThe executor function in the constructor will be executed immediately and accepts two parameters, resolve and reject, which are used to modify the state of the Promise.
๐ŸžThe resolve method is used to modify the Promiseโ€™s status from โ€œpendingโ€ to โ€œfulfilledโ€ and pass the value to subsequent handlers.
๐ŸฅšThe reject method is used to modify the Promiseโ€™s status from โ€œpendingโ€ to โ€œrejectedโ€ and pass the reason to the subsequent handler.
๐ŸณThe then method is used to register a callback function to be executed when the Promise is completed or rejected. It accepts two parameters: onFulfilled and onRejected, which are called when the Promise is completed or rejected respectively.
๐ŸฅฉThe then method returns a new MyPromise instance to support chained calls. If onFulfilled or onRejected returns a value, it will be used as the resolved value for the next MyPromise instance.
๐Ÿ”The catch method is the shorthand form of then(null, onRejected).
๐Ÿ•The isFulfilled method is used to check whether the Promise is in the fulfilled state.
๐ŸŒฎThe isRejected method is used to check whether the Promise is in the rejected state.
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ‘4
This media is not supported in your browser
VIEW IN TELEGRAM
๐Ÿ’ข Interview Questions: Event loop mechanism

#js #interview #eventLoop

Question: Give an explanatory note on the event loop mechanism.

answer:

The event loop mechanism mainly has the following processes:

๐Ÿ‰Synchronous tasks are executed on the main thread, forming an execution context stack.

๐Ÿ“Once all synchronous tasks in the execution stack have been executed, the system will read the asynchronous tasks in the queue, such as Promise.then(), setTimeout, AJAX callbacks, etc.

๐Ÿ†The asynchronous task will be added to the task queue

๐Ÿฅ‘Once the execution stack is cleared, the system checks the task queue. If it is not empty, the first task is taken out and placed on the execution stack for execution.

๐ŸฅฆThe main thread repeats the process of alternating execution of the stack and queue, thereby realizing queued execution of threads.
The event loop allows synchronous tasks and asynchronous tasks to be executed alternately in the same thread, making full use of CPU resources. This is important for JavaScript that supports UI interaction and responsiveness.
Please open Telegram to view this post
VIEW IN TELEGRAM
โค1๐Ÿ‘1
๐Ÿšณ Interview Questions: Ajax implementation

#js #interview #ajax

Implement an ajax request function that supports Promise:

๐ŸฃSend a request using the XMLHttpRequest object

๐Ÿ›Initialize the open method, configure the request method and url

๐ŸทAdd onload and onerror callback functions

๐Ÿง‰onload determines whether the status code is within the range of 200โ€“300 resolve, otherwise reject

๐Ÿซ˜onerror directly reject

๐ŸฅงAfter the request is successful, resolve returns response, and after failure, reject reports an error.

๐ŸฅŸSupport options to configure request parameters and request body

๐ŸฑReturns a Promise object, which can be processed externally using then/catch

Analysis: Promise is used to encapsulate asynchronous ajax requests and achieve a synchronous programming style.
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿšณ Interview Questions: JSONP cross-domain implementation

#js #interview #jsonp

Analysis: Create the script node script.src, set the callback function callbackName, parse the parameters and splice the URL, dynamically insert it into the body to implement JSONP cross-domain request, and return the Promise interface.
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ“ต Interview Questions: Implement deep cloning

#js #interview #deepClone

Analysis: Recursively implement deep cloning of objects and arrays, returning basic types directly, and reference types recursively call deep cloning hierarchically.
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿšฑ Interview Questions: Function currying

#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
๐Ÿšณ Interview Questions: Implement promise.all method

#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
๐Ÿ“ต Interview Questions: Implement Instanceof

#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
๐Ÿšฑ Interview Questions: Implement a debounce anti-shake function

#js #interview #debounce
Please open Telegram to view this post
VIEW IN TELEGRAM