Original link: dsx2016.com/?p=692

Wechat official account: Big Brother 2016

ECMAScript

ECMA Script is the name of the ecMA-262 standardized scripting language.

Although JavaScript and JScript are ECMAScript compatible, they contain capabilities beyond ECMAScript.

The following table is grouped according to four modules:

  • ECMAScript 1st Edition (ECMA-262)Corresponds to the first edition andES5Previous version (this article only)
  • ECMAScript 5.1 (ECMA - 262)The correspondingES5
  • ECMAScript 2015 (6th Edition, ECMA-262)The correspondingES6
  • ECMAScript LasterThe correspondingES7/ES8/ES9/ES10Etc.

Array.prototype.map() = map() and so on.

ES 1st ES5 ES6 ES Laster
Array.length Array.isArray() Array.from() includes()
unshift() indexOf() Array.of()
concat() lastIndexOf() findIndex()
join() every() find()
pop() some() fill()
push() forEach() copyWithin()
reverse() map() entries()
shift() filter() flat()
slice() reduce() flatMap()
sort() reduceRight() includes()
splice() keys()
toLocaleString() values()
toString()

Removes duplicates from an array

There are two ways to use ES6, both of which require first creating a Set data structure to remove duplicate values

let fruits=[`banana`.`orange`.`apple`.`pear`.`grape`.`apple`];
Copy the code

First, use array.from ()

let deduplicationFruits=Array.from(new Set(fruits))
Copy the code

The second way is to use… Extended operators (recommended)

let deduplicationFruits=[...new Set(fruits)]
Copy the code

Determines whether the array has a given value

Array.prototype.indexof ()

let arr=[1.2.NaN]
if (arr.indexOf(1)! = =- 1) {
  // ...
}
/ / note
[NaN].indexOf(NaN) // -1
Copy the code

Use array.prototype.includes ()

let arr=[1.2.NaN]
arr.includes(1) // true
[NaN].includes(NaN) // true
Copy the code

Check if it is an array

ES5 uses array.isarray () (recommended), other methods are not described

const arr = [];
const obj = {};
Array.isArray(arr);//true
Array.isArray(obj);//false
Copy the code

Shallow copy and deep copy

Shallow copy

ES6 use… Extended operator

Note: This method is a shallow copy

let arr1=[1.2.3, {a:123}];
let arr2=[...arr1] 
arr2[0] =5 / / arr1 [1, 2, 3, {a: 123}] arr2 [5, 2, 3, {a: 123}].
arr2[3].a=456 / / arr1 [1, 2, 3, {a: 456}] arr2 [5, 2, 3, {a: 456}].
Copy the code

Deep copy

Using JSON serialization, json.parse (json.stringify (ARR)) is relatively simple and can be used for general scenarios.

This method is not compatible with complex properties of objects, such as set, get, Function, etc., and is only compatible with data types supported by JSON format.

Regular expression types and function types cannot be deeply copied, and the corresponding values are directly lost

let arr1=[1.2.3, {a:123}]
let arr2=JSON.parse(JSON.stringify(arr1))
Copy the code