Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are 10 super practical Javascript techniques like?

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about 10 super practical Javascript skills. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

As we all know, JavaScript has been changing rapidly. There are a lot of great features in the new ES2020, and most of us can't wait to try. To be honest, sometimes we can write code from different angles, and we can also achieve the same effect, and some will be shorter and clearer.

1. Method parameter verification

You can set default values for function parameters in ES6, and with this, we can implement a clever technique for verifying that method parameters cannot be empty.

Const isRequired = () = > {throw new Error ('param is required')} const print = (num = isRequired ()) = > {console.log (`printing ${num} `)} print (2) / / printing 2 print () / / error print (null) / / printing null

two。 Format JSON code

We are all very familiar with JSON.stringify, but what we rarely know is that it can also produce formatted output.

The stringify method has three parameters: value,replacer and space. Among them, the last two are optional parameters, which is why we seldom know about them. To indent JSON, you must use the space parameter.

Console.log (JSON.stringify ({name: "John", Age:23}, null,'\ t'); > > {"name": "John", "Age": 23}

3. Get a unique value from the array

To get unique values from an array, you need to filter out duplicate values using the filter method. But with the new Set native object, things become very simple.

Let uniqueArray = [. New Set ([1,2,3 ball',false,false,true,true 3,3, "school", "school", 'ball',false,false,true,true]); > [1,2,3, "school", "ball", false,true]

4. Remove an imaginary value from an array

In some cases, we want to remove imaginary values from the array. An imaginary value is a value in JavaScript whose value is FALSE. There are only six imaginary values in JavaScript. They are:

Undefined

Null

NaN

0

'' (empty character)

False

The easiest way to filter these virtual values is to use the following function:

MyArray.filter (Boolean)

If you want to make some changes to the array and then filter the new array, you can try this. Keep in mind that the original myArray remains the same.

MyArray .map (item = > {/ / Do your changes and return the new item}) .filter (Boolean)

5. Merge multiple object

In actual requirements, we often need to merge multiple comparisons, and a quick way to do this is as follows:

Const user = {name: 'John Ludwig', gender:' Male'} const college = {primary: 'Mani Primary School', secondary:' Lass Secondary School'} const skills = {programming: 'Extreme', swimming:' Average', sleeping: 'Pro'} const summary = {. User,... college,... skills}

6. Sort an array of numbers

The JavaScript array has a built-in sort method. By default, this sort method converts array elements to strings and sorts them lexicographically. But there will be a problem when sorting an array of numbers, and the solution is to write a sorting method by hand.

[0meme10, 4, 9, 123, 54, 1] .sort ((a _ r _ b) = > a _ r é b) > [0me 1,4 ~ (th), 9,10,54,123]

7. Disable the right mouse button

In some cases, we want to disable the right mouse button on a web page, which can be disabled in the following ways:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report