Zhaotong java Training School tells you JavaScript programming and development skills?

With the continuous development of the Internet, the use of JavaScript programming language has also been mastered by programmers. Today we will take a look at some skills of JavaScript programming and development.

JSON.stringify

We often use JSON objects. For example, when we want to make a deep copy of an object, we can use JSON.stringify and JSON.parse of JSON object to copy the same object without referring to the original object. It will also be used when using localStorage, because localStorage can only store the contents in string format, so we convert the values into JSON strings before saving them, and then convert them into objects or arrays when taking them out for use.

Parameters:

Value: the value of the variable to serialize.

Replacer: Replacer. It can be a function or an array. If it is a function, every attribute of value must be processed by this function, and the return value of this function is the serialized value. If it is an array, the elements of the array are required to be strings, and these elements will match the keys of values, and the result of serialization will only contain the values of each element in the array as keys.

Space: specifies the code indentation of the output value, which is used to beautify the format, and can be a number or a string. If it is a number (greater than 10), it represents how many spaces each line of code is indented. If it is a string, the string (more than the first ten characters) will be displayed before each line of code.

Using Set to achieve array deduplication

In ES6, a new data structure type Set is introduced. The structures of sets and arrays are very similar, and sets and arrays can be converted to each other.

Array deduplication is also a common front-end interview problem, and there are many methods, so I won't go into details here. Let's take a look at array deduplication ... (extended operator) using Set and.

Avoid naming conflicts with block-level ranges.

In the development process, we usually encounter the problem of naming conflict, that is, we need to define different values according to different scenarios to assign values to the same variable. The following Zhaotong IT training/introduces a method to solve this problem by using the block-level scope in ES6.

Functional parameter value verification

We know that in ES6, the default values of parameters are added to the function, and some default values can be set for parameters, which can make the code more concise and more maintainable.