当前位置:网站首页>7 killer JS lines of code

7 killer JS lines of code

2022-06-27 13:00:00 gaoryrt

JavaScript It is the most critical pillar in network development .

This article contains code snippets handpicked from sterile gloves , And put it on a Satin Pillow .
One by 50 A team of people examined the code , And ensure it is in a highly polished state before release . Our article publishing expert from Switzerland lit a candle , When he typed the code into the best gold inlaid keyboard money could buy , The crowd was booed .
We all have a wonderful celebration , The whole Party marched down the street to the cafe , The entire town of Kolkata waved as the article was posted online " i wish you a happy voyage !".

I wish you a happy reading !

Array scrambling

When using algorithms that require some degree of randomization , You will often find that shuffling arrays is a very necessary skill . The following clip is O(n log n) To shuffle an array in place .

const shuffleArray = (arr) => arr.sort(() => Math.random() - 0.5) .
//  test 
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(shuffleArray(arr)).

Copy to clipboard

stay Web In the application , Copy to clipboard is rapidly becoming popular because of its convenience to users .

const copyToClipboard = (text) =>
  navigator.clipboard?.writeText && navigator.clipboard.writeText(text).
//  test 
copyToClipboard("Hello World!").

Be careful : according to caniuse, The method is right 93.08% Effective for global users . Therefore, you must check whether the user's browser supports this API. To support all users , You can use an input and copy its contents .

Array weight removal

Each language has its own Hash list implementation , stay JavaScript in , It is called Set. You can use Set Data structures easily get unique elements from an array .

const getUnique = (arr) => [...new Set(arr)].
//  test 
const arr = [1, 1, 2, 3, 3, 4, 4, 5, 5];
console.log(getUnique(arr)).

Detect dark mode

With the popularity of dark mode , If the user has enabled dark mode on their device , So it's ideal to switch your application to dark mode . Fortunately, , Media queries can be used to make this task simple .

const isDarkMode = () =>
  window.matchMedia &&
  window.matchMedia("(prefers-color-scheme: dark)").matches.
//  test 
console.log(isDarkMode()).

according to caniuse The data of ,matchMedia The support rate is 97.19%.

Scroll to top

Beginners often find it difficult to scroll elements correctly . The simplest way to scroll elements is to use scrollIntoView Method . Add behavior ."smooth " To achieve smooth scrolling animation .

const scrollToTop = (element) =>
  element.scrollIntoView({ behavior: "smooth", block: "start" }).

Scroll to the bottom

It's like scrollToTop The method is the same ,scrollToBottom The method can also be used scrollIntoView The method is easy to implement , Just switch the block value to end

const scrollToBottom = (element) =>
  element.scrollIntoView({ behavior: "smooth", block: "end" }).

Generate random colors

Does your application rely on random color generation ? Don't look anymore , The following code snippet can meet your requirements

const generateRandomHexColor = () =>
  `#${Math.floor(Math.random() * 0xffffff) .toString(16)}`;

Pay attention to me , Learn about the latest weekly news in the technology field
Need a top rated front-end development freelancer to solve your development problems ? Please be there. Upwork Contact me
Want to see what I'm doing ? Please check my Personal website and GitHub
Want to contact me ? Please be there. LinkedIn Contact me on
I'm a freelancer , Will be in 2022 He began to be a digital nomad in the middle of the year . Want to know about my journey ? stay Instagram Pay attention to me

原网站

版权声明
本文为[gaoryrt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202161641524386.html