Extension of JS ES6 built-in objects

String expansion

Template string
Put the string in backquotes: `in
1, repeat Repeat operations on the string

let str1 = 'a';
let str2 = str1.repeat(6);
console.log(str2); // aaaaaa

2. includes() Determine whether there is a specified string in the string
startsWith() Find whether there is a specified string at the beginning
endsWith() Find whether there is a specified string at the end

let str = 'hello';

console.log(str.includes('ll')); // true
console.log(str.includes('eee')); // false

console.log(srr.startsWith('h')); // true
console.log(srr.startsWith('a')); // false

console.log(srr.endsWith('o')); // true
console.log(srr.endsWith('a')); // false

Array expansion

Array.from() turns array-like objects into real arrays

Array.of() creates an array

find() findIndex() filters the array, the former is not found as undefined, the latter is not found as -1, and the subscript is returned correctly

const arr = [1,2,3,4,5];
let res = arr.find(function (a){
    
    
	return a < 2;
});
console.log(res); // 1

let res = arr.findIndex(function (a){
    
    
	return a < 2;
});
console.log(res); // 0

fill() fills the array given a value

const arr = [1,2,3,4,5];
arr.fill('aaa');
console.log(arr); // ["aaa","aaa","aaa","aaa","aaa"]
arr.fill('aaa', 1, 3);
console.log(arr); // [1,"aaa","aaa",4,5]

Spread operator (spread)

arr = [1, 2, 34, 5, 67]
arr1 = [...arr] // ...就可取到 arr 里所有的值 

Object extension

The concise representation of the object is the
same key and value can be abbreviated

const obj = {
    
    a:a}

Shorthand

const obj = {
    
    a}

Object.is() comparison

console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(+0, -0)); // false

Object.assign() is used to merge multiple objects and copy all enumerable properties of the source object to the target object

let obj1 = {
    
    a: 1};
let obj2 = {
    
    a: 2, b: 3};
let obj3 = {
    
    c: 'abc'};
Object.assign(obj1, obj2, obj3); // 第一个属性是目标对象,第二个后面的是要合并的,若后有重则盖前
console.log(obj1); // {a: 2, b: 3, c: 'abc'} 

Guess you like

Origin blog.csdn.net/weixin_43176019/article/details/109178111