This content originally appeared on Level Up Coding - Medium and was authored by Maxwell
8 Javascript methods that are often used in work, remember these methods and avoid reinventing the wheel.
1. Back to Top
When the page is very long, if the user wants to go back to the top of the page, he must scroll the scroll key several times to go back to the top. If there is a “return to top” button in the lower right corner of the page, the user can click to go back to the top. For users, it is a better experience.
2. Copy text to clipboard
A very common need when building websites is the ability to copy text to the clipboard with the click of a button.
3. Debouncing/Throttling
In the process of front-end development, we will encounter many buttons that are clicked frequently, and then trigger multiple events, but we do not want to trigger events too frequently. Here are two common solutions to prevent Debouncing and Throttling.
Basic Introduction
Debouncing: Frequently trigger an event within a specified time, whichever is the last trigger.
Throttling: An event is triggered frequently within a specified time, and it will only be triggered once.
Application Scenarios
Debouncing: Input search, when the user is constantly inputting content, use anti-shake to reduce the number of requests and save request resources.
Throttling: The scene is generally a button click. 10 clicks in one second will initiate 10 requests. After throttling, if you click multiple times in 1 second, it will only be triggered once.
4. Initialize the array
fill() : It is a new method in ES6. Filling the array with the specified element is actually initializing the array with the default content.
5. Check if it is a function
Detecting whether it is a function In fact, it is good to write isFunction directly after writing, so as to avoid repeated writing judgments.
6. Check if it is a safe array
Check if it is a safe array, if not, return an empty array here with the isArray method.
7. Check if an object is a secure object
8. Filter special characters
Use regular expressions in js to filter special characters and check whether all input fields contain special symbols.
8 tools and methods often used in JavaScript was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Maxwell
Maxwell | Sciencx (2022-10-19T11:44:00+00:00) 8 tools and methods often used in JavaScript. Retrieved from https://www.scien.cx/2022/10/19/8-tools-and-methods-often-used-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.