ES6 of front-end interview questions

1.ES6 is the next generation standard of js language and was released in June 2015

2.let const

Let declared variables are only valid in the code block where the let command is located

let can only be declared once

let does not exist. Variable promotion will have a temporary dead zone. If the dead zone is typeof, an error will be reported

const declares a read-only constant. Once declared, the value of the constant cannot be changed. Once declared, it must be initialized, otherwise an error will be reported.

The constant value is the same address. If it is the data in the object array, it can be changed.

That is, if

const arr =[],obj={}
arr.push({obj})//That's OK
arr = new Array[]Just can't, because it will change the address
 So is the object

3. Function default value

That's it

function fun (x,y ='111'){
	//Here y has a default value

}

4. Deconstruction assignment

let [x,y,z] = [1,2,3]
let {username,pasword} = {username:"11",pasword:'1111'}
If not on the right undefined
 let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
    baz // "aaa"
    foo // error: foo is not defined
        //In the above code, foo is the matching pattern and baz is the variable. What is really assigned is the variable baz, not the pattern foo.

5. Extension operator

Three points: expand the array or class array object into a series of values separated by commas

You can merge arrays and objects

6.rest operator

... add a lot of parameters

Is to combine these parameters into an array

rest can only be the last parameter

7. Template string

The array {AAA} contained in curly braces is enclosed in backquotes

8. Arrow function

Features:

Do not bind this value: inherit this from the upper layer of the scope chain

Cannot be used as a constructor

No arguments class array (can be replaced by rest parameter)

Application: is an anonymous function

()=> aaa  amount to ()=>{return aaa}
aa=>{} amount to(aaa)=>{}
()=>{}

//This example
var Person = {
    'age': 18,
    'sayHello': ()=>{
        console.log(this.age);
      }
};
var age = 20;
Person.sayHello();  // 20
//The scope is inside a function, and there is no function outside the arrow function, so it is global, so it is global. A will output 20. Note that the scope is for the function

9. One of the extensions of array is to convert an array into an array

arguments is not an array and does not inherit array Properties and methods on the prototype object, but with the length attribute,

Convert a class array to an array

Array.prototype.slice.call(arguments)

Array.from() turns the class array into a real array

Array.of() turns a set of values into an array

10. Extension of array prototype method

find() finds the qualified elements in the array. If there are multiple qualified elements, it returns the first one.

let arr = Array.of(1, 2, 3, 4);
console.log(arr.find(item => item > 2)); // 3

findIndex() finds the subscript of the qualified element in the array. If there are more than one, it returns the first one.

fill() fills an array element within a range of indexes with a value

let arr = Array.of(1, 2, 3, 4);
// Parameter 1: value used to populate
// Parameter 2: start index to be filled
// Parameter 3 (optional): the end index to be filled. It defaults to the end of the array
console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]

copyWithin()

Modifies an array element with a range index to another element with a specified range index for this array.

// Parameter 1: modified start index
// Parameter 2: the starting index of the data to be overwritten
// Parameter 3 (optional): the end index of the data to be overwritten. The default is the end of the array
console.log([1, 2, 3, 4].copyWithin(0,2,4)); // [3, 4, 3, 4]
 
// Parameter 1 is a negative number, indicating the reciprocal
console.log([1, 2, 3, 4].copyWithin(-2, 0)); // [1, 2, 1, 2]
 
console.log([1, 2, ,4].copyWithin(0, 2, 4)); // [, 4, , 4]

Extended method

lookup

find()

Find the qualified elements in the array. If there are multiple qualified elements, the first element will be returned.

let arr = Array. of(1, 2, 3, 4); console. log(arr.find(item => item > 2)); // 3 / / array vacancy is processed as undefined console log([, 1].find(n => true)); // undefined

findIndex()

Find the qualified element index in the array. If there are multiple qualified elements, return the first element index.

let arr = Array.of(1, 2, 1, 3); // Parameter 1: callback function / / parameter 2 (optional): specify this value in the callback function console log(arr.findIndex(item => item == 2)); // 1 / / array vacancy is processed as undefined console log([, 1].findIndex(n => true)); // 0

fill

fill()

Populates the contents of array elements with a range of indexes into a single specified value.

let arr = Array.of(1, 2, 3, 4); // Parameter 1: the value to be filled. / / parameter 2: the start index to be filled. / / parameter 3 (optional): the end index to be filled. The default value is console. At the end of the array log(arr.fill(0,1,2)); // [1, 0, 3, 4]

copyWithin()

Modifies an array element with a range index to another element with a specified range index for this array.

//Parameter 1: the modified start index. / / parameter 2: the start index of the data to be overwritten. / / parameter 3 (optional): the end index of the data to be overwritten. The default is console at the end of the array log([1, 2, 3, 4].copyWithin(0,2,4)); // [3, 4, 3, 4] / / parameter 1 is a negative number, indicating the reciprocal console log([1, 2, 3, 4].copyWithin(-2, 0)); // [1, 2, 1, 2] console. log([1, 2,4].copyWithin(0, 2, 4)); // [, 4, 4]

ergodic

entries()

Traverse key value pairs.

for(let [key, value] of ['a', 'b'].entries()){
    console.log(key, value);
}
// 0 "a"
// 1 "b"
 
// Do not use for Of cycle
let entries = ['a', 'b'].entries();
console.log(entries.next().value); // [0, "a"]
console.log(entries.next().value); // [1, "b"]
 
// Array contains empty bits
console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]

keys()

Traverse key names.

for(let key of ['a', 'b'].keys()){    console.log(key);}// 0 / / 1 / / the array contains empty console log([...[,'a'].keys()]); //  [0, 1]

values()

Traverse key values.

contain

includes()

Whether the array contains the specified value.

Nested array to one-dimensional array

flat()

console.log([1 ,[2, 3]].flat()); // [1, 2, 3] / / specify the number of nested layers to convert console log([1, [2, [3, [4, 5]]]].flat(2)); //  [1, 2, 3, [4, 5]] / / no matter how many layers of console are nested log([1, [2, [3, [4, 5]]]].flat(Infinity)); //  [1, 2, 3, 4, 5] / / automatically skip the empty console log([1, [2, 3]].flat());< p> // [1, 2, 3]

11. Object extension

Concise representation of objects

 //Example 1
    const foo = 'bar';
    const baz = { foo };
    baz // {foo: "bar"}

    // Equivalent to
    const baz = { foo: foo };

    //Example 2
    const o = {
      method() {
        return "Hello!";
      }
    };
    // Equivalent to
    const o = {
      method: function () {
        return "Hello!";
      }
    };



    //Example 3
    let birth = '2000/01/01';

    const Person = {
      name: 'Zhang San',
      //Equivalent to birth: birth
      birth,
      // Equivalent to hello: function()
      hello() {
        console.log('My name is', this.name);
      }
    };

Attribute name

obj.foo = true
obj['a'+'bc'] =123

let propKey = 'foo'
let obj = {
	[propKey]:true,
	['a'+'bc']:123
}

Static method

Object.assign(target, source_1, ···)

Used to copy all enumerable properties of the source object to the target object.

let target = {a: 1};
let object2 = {b: 2};
let object3 = {c: 3};
Object.assign(target,object2,object3);  
// The first parameter is the target object, and the following parameter is the source object
target;  // {a: 1, b: 2, c: 3

Object.is(value1, value2)

It is used to compare whether two values are strictly equal, which is basically similar to (= =).

12.symbol

Here we will talk about data types

Basic data type number string boolean undefined null

The reference data type is Object (Array FUnction date Error)

This symbol is a unique value

var S1 = Symbol()

13.for in traversal

Get key name

14.for of traversal

Get property value

15. Many traversal methods

forEach() is used to traverse the array

arr.forEach((item,index)=>{

})

for in is es5. Traverse the object array to get the key value. It will traverse the prototype attribute

You can use hasOwnPropery to determine whether it is your own attribute

for of is es6 used to traverse the elements in the array

Object.keys () returns an array whose elements are a collection of all key names of the input object

16.Set

Is a constructor that generates a Set through new without duplicate values

17.Map

Similar objects are collections of key value pairs, but the range of keys is not limited to strings. Objects can also be keys

18.promise

19.async await

Keywords: Javascript

Added by Sharif on Thu, 06 Jan 2022 02:02:18 +0200