1. Let and block-level scope

JS scope: global scope, function scope. There is no concept of block scope. Block-level scopes are new in ECMAScript 6. Block scopes are included by {}. The {} in the if and for statements are also block scopes.

var elements = [{}, {}, {}]; for (var i=0; i < elements.length; i++) { elements[i].onclick = function() { console.log(i); } } elements[1].onclick(); //// PS F:\ lagou> node.\task1\task1.js 3 3 3Copy the code

1.1 Through closures

var elements = [{}, {}, {}]; for (var i = 0; i < elements.length; i++) { elements[i].onclick = (function (i) { return function () { console.log(i); } })(i) } elements[0].onclick(); elements[1].onclick(); elements[2].onclick(); //// PS F:\ lagou> node.\task1\task1.js 0 1 2Copy the code

1.2 use the let

var elements = [{}, {}, {}]; for (let i = 0; i < elements.length; i++) { elements[i].onclick = function () { console.log(i); } } elements[0].onclick(); elements[1].onclick(); elements[2].onclick(); //// PS F:\ lagou> node.\task1\task1.js 0 1 2Copy the code

2. Array deconstruction

Const arr = [100200300]; const [a,b,c] = arr; console.log(a,b,c); //// PS F:\ lagou> node.\task1\task1.js 100 200 300Copy the code

2.1 Defining only one variable requires leaving other positions empty

Const arr = [100200300]; const [,,c] = arr; console.log(c); //// PS F:\ lagou> node.\task1\task1.js 300Copy the code

2.2 use… Define the remaining variables

It can only be defined in the last position

Const arr = [100200300]; const [a, ...rest] = arr; console.log(rest); //// PS F:\ lagou> node.\task1\task1.js [200, 300]Copy the code
Const arr = [100200300]; const [...rest] = arr; console.log(rest); //// PS F:\ lagou> node.\task1\task1.js [100, 200, 300]Copy the code
Const arr = [100200300]; const [...rest,c] = arr; console.log(rest); / / / / F: \ retractor front-end \ lagou \ task1 \ task1 js: 15 const /... rest, c = arr; ^ ^ ^ ^ ^ ^ ^Copy the code

2.3 Assign initial values during deconstruction

const arr = [100, 200, 300]; const [a, b, c = 123, d = 'defaultValue'] = arr; console.log(c, d); //// PS F:\ lagou> node.\task1\task1.js 300 defaultValueCopy the code

2.4 Deconstruction example: Parse the path

// Const path = '/foo/bar/baz'; const temp = path.split('/'); const rootdir = temp[1]; console.log(temp); console.log(rootdir); / / / / PS F: \ retractor front-end \ lagou > node. \ task1 \ task1 js [' and 'foo', 'bar', 'baz'] fooCopy the code
// const path = '/foo/bar/baz'; const [,rootdir] = path.split('/'); console.log(rootdir); //// PS F:\ lagou> node.\task1\task1.js fooCopy the code

3. Deconstruction of objects

3.1 Objects are defined by keys and arrays by subscripts

const obj = {name:'yangzhen' , age:24}; const { name , age , sex } = obj; console.log(sex,name,age); //// PS F:\ lagou> node.\task1\task1.js undefined yangzhen 24Copy the code

3.2 Do not Use aliases

const obj = {name:'yangzhen' , age:24};

const name = 'jack';

const { name , age , sex } = obj;

console.log(sex,name,age);

////
const { name , age , sex } = obj;
        ^
SyntaxError: Identifier 'name' has already been declared

Copy the code

3.3 Assigning an Initial Value using an Alias

const obj = {name:'yangzhen' , age:24}; const name = 'jack'; const { name:myname , age , sex = 18 } = obj; console.log(sex,myname,age); //// PS F:\ lagou> node.\task1\task1.js 18 Yangzhen 24Copy the code

4. String extension methods


const { log } = console

const message = 'Error: foo is noe defined.'

log (
    message.startsWith('Error'),
    message.endsWith('.'),
    message.includes('is')
)

////
true true true

Copy the code

5. Default values of function parameters

5.1 Primitive Methods (for Boolean types)

Function foo(enable) {// Primitive method (for Boolean types) enable = enable === undefined? true : false console.log(enable) } foo() //// true foo(false) //// falseCopy the code

5.2 Primitive Methods (for other types)

/ / function parameter of the default function foo (enable) {/ / original methods enable = enable | | 0 console. The log (enable)} foo () / / / / 0 foo (5) / / / / 5Copy the code

5.3 ES6 grammar

function foo(enable = true){
    console.log(enable)
}

foo() //// true
foo(false) //// false

Copy the code

6. Remaining parameters

6.1 Method 1: Use Arguments

Function foo(){console.log(arguments)} foo(1,2,3,4) //// PS E:\lagou front-end \demo> node.\part1\test1.js [arguments] {'0': 1, '1': 2, '2': 3, '3': 4}Copy the code
Function foo(){const [a,b,c,d] = arguments console.log(a,b,c,d)} foo('a',2,3,4) //// PS E:\lagou front-end \demo> node .\part1\test1.js a 2 3 4Copy the code

6.2 Method 2: Using…

. Args can only be used once at the end

function foo(first,... Args){console.log(first) console.log(args)} foo('a',2,3,4) //// PS E:\lagou front-end \demo> node.\part1\test1.js a [2,3, 4]Copy the code

6.3 ·· Expand the array

Const arr = [1,2,3,4] console.log(... Arr) //// PS E:\lagou front-end \demo> node.\part1\test1.js 1 2 3 4Copy the code

7. Arrow function and this

Normal functions: the direction of this is not determined when the function is created, but only when the function is called.

The arrow function has no prototype, so the arrow function itself has no this. The this inside the arrow function is lexical scoped, depending on the context, and refers to this, which at definition inherits from the first normal function in the outer layer. The this object inside the function is the object at which it is defined, regardless of the object at which it is used.

const name = 'jack' const person = { name: 'tom', sayHi: Function (){console.log(' my name is ${this.name} ')}} person.sayhi () //// PS E:\lagou front-end \demo> node.part1 \test1.js my name is tomCopy the code
const name = 'jack' const person = { name: 'tom', sayHi: Function (){console.log(' my name is ${name} ')}} person.sayhi () //// PS E:\lagou front-end \demo> node.part1 \test1.js my name is jackCopy the code

7.1 Using the arrow function

The arrow function does not change the direction of this

Const person = {name: 'Tom ', sayHi: () => {console.log(' my name is ${this.name} ')}} person.sayhi () //// PS E:\lagou front-end \demo> node.part1 \test1.js my name  is undefinedCopy the code
const name = 'jack' const person = { name: 'tom', sayHi: () => { console.log(`my name is ${name}`) } } console.log(this) console.log(this.name) person.sayHi() //// PS E:\lagou front-end \demo> node.\part1\test1.js {} undefined my name is jackCopy the code

7.2 Traditional _this Usage

const person = { name: 'tom', sayHi: function() { console.log(`my name is ${this.name}`) }, sayHiAsync:function(){ setTimeout(function () { console.log(`my name is ${this.name}`) }, 1000); }} person.sayhi () person.sayhiasync () //// PS E:\lagou front-end \demo> node.part1 \test1.js My name is Tom my name is undefinedCopy the code
const person = { name: 'tom', sayHi: function() { console.log(`my name is ${this.name}`) }, sayHiAsync:function(){ _this = this; setTimeout(function () { console.log(`my name is ${_this.name}`) }, 1000); }} person.sayhi () person.sayhiasync () //// PS E:\lagou front-end \demo> node.part1 \test1.js My name is Tom my name is TomCopy the code

7.3 setTimeout Using the arrow function can avoid _this

The this inside the arrow function is lexical scoped, determined by context

const person = { name: 'tom', sayHi: () => { console.log(`my name is ${this.name}`) }, sayHiAsync:function(){ setTimeout(()=> { console.log(`my name is ${this.name}`) }, 1000); }} person.sayhi () person.sayhiasync () //// PS E:\lagou front-end \demo> node.part1 \test1.js my name is undefined my name is tomCopy the code

8. Object literals are enhanced

const bar = '345' const obj = { foo: 123, //bar:bar bar, //method1:function(){ // console.log(this) //} method1(){ console.log(this) }, [math.random ()]:456} // In traditional methods, Object dynamically adds property obj[math.random ()] = 123 console.log(obj) obj.method1() //// PS E:\lagou front-end \demo> node.\part1\test1.js {foo: {foo: 123, bar: '345', method1: [Function: method1], '0.8616180359346404': 123} {foo: 123, bar: '345', method1: [Function: Method1], '0.8616180359346404' : 123}Copy the code

9. Object extension method

9.1 Object.assignCopies properties from multiple source objects to the target object

const source1 = { a:123, b:123 } const source2 = { b:789, d:789 } const target = { a:456, c:456 } const result = Object.assign(target,source1,source2) console.log(target) console.log(result === target) //// PS E:\lagou front-end \demo> node.\part1\test1.js {a: 123, C: 456, b: 789, d: 789} trueCopy the code

9.2 case

function func(obj){

    obj.name = 'func obj'
    console.log(obj)
}

const obj = {name: 'global obj'}

func(obj)
console.log(obj)

////
{ name: 'func obj' }
{ name: 'func obj' }
Copy the code
function func(obj){
    const funObj = Object.assign({},obj)
    funObj.name = 'func obj'
    console.log(funObj)
}

const obj = {name: 'global obj'}

func(obj)
console.log(obj)

////
{ name: 'func obj' }
{ name: 'global obj' }
Copy the code

10. Proxy Proxy object

const person = { name: 'yang', age: // Parameter 1: Proxy object // Parameter 2: Proxy object const personProxy = new Proxy(person,{// Get method to monitor attribute access // parameter 1: Proxy target object // parameter 2: attribute name accessed externally get(target,property){ console.log(target,property) return 100 }, Set (){}}) console.log(personproxy.name) //// PS E:\lagou front-end \demo> node.\part1\test1.js {name: 'yang', age: 18 } name 100Copy the code
const person = { name: 'yang', age: // Parameter 1: Proxy object // Parameter 2: Proxy object const personProxy = new Proxy(person,{// Get method to monitor attribute access // parameter 1: Proxy target object // parameter 2: attribute name accessed externally get(target,property){ return property in target ? target[property] : 'default' }, Set (){}}) console.log(personproxy.name) console.log(personproxy.xxxx) //// PS E:\lagou front-end \demo> node .\part1\test1.js yang defaultCopy the code
const person = { name: 'yang', age: // Parameter 1: Proxy object // Parameter 2: Proxy object const personProxy = new Proxy(person,{// Get method to monitor attribute access // parameter 1: Proxy target object // parameter 2: attribute name accessed externally get(target,property){ return property in target ? Target [property] : 'default'}, // Use the set method to set the property //1. Set (target,property,value){if(property === 'age') {if(! Number.isInteger(value)) { throw new TypeError(`${value} is not an int`) } } target[property] = value } }) Personproxy.age = 30 personproxy.sex = 'man' console.log(personProxy) //// PS E:\lagou front-end \demo> node.\part1\test1.js { name: 'yang', age: 30, sex: 'man' }Copy the code

11. The proxy and the Object. DefineProperty ()

11.1 Object.defineProperty() can only listen for reads and writes of objects

const person = { name: 'yang', age: 18 } const personProxy = new Proxy(person,{ deleteProperty(target,property){ console.log('delete',property) delete Target [property]}}) delete personproxy.age console.log(person) //// PS E:\lagou front-end \demo> node.\part1\test1.js delete age { name: 'yang' }Copy the code

11.2 Proxy better supports array object monitoring

const list = [] const listProxy = new Proxy(list,{ set(target,property,value) { console.log('set',property,value) Target [property] = value return true}}) listproxy.push (100) Listproxy.push (200) Console. log('list',list) //// PS E:\lagou front-end \demo> node. \part1\test1.js set 0 100 set length 1 set 1 200 set length 2 list [ 100, 200 ]Copy the code

12.Reflect unified object manipulation API

A static class that encapsulates a set of low-level operations on an object

The Reflet member method is the default implementation of Proxy handling objects

12.1 remove the Reflect

const person = { name: 'yang', age: 18 } const personProxy = new Proxy(person,{ get(target,property) { console.log('watch logic') // return Reflect.get(target,property)}}) console.log(personproxy.age) //// PS E:\lagou front-end \demo> node.\part1\test1.js watch logic undefinedCopy the code

12.2 add Reflect

const person = { name: 'yang', age: 18 } const personProxy = new Proxy(person,{ get(target,property) { console.log('watch logic') return Reflect.get(target,property)}}) console.log(personproxy.age) //// PS E:\lagou front-end \demo> node.\part1\test1.js watch logic 18Copy the code

Reflect does what it does: Provide a unified set of apis for manipulating objects

12.3 Not Using Reflect’s Operation on an Object

const person = { name: 'yang', age: Log ('name' in person) // Get all the Key console.log(object.keys (person)) // delete the properties of the Object Console. log(delete person['age']) console.log(person) //// PS E:\lagou front-end \demo> node.\part1\test1.js true [' name', 'age' ] true { name: 'yang' }Copy the code

12.4 Operation on an Object Using Reflect

const person = { name: 'yang', age: Log (reflect. has(person,'name')) // Get all the Key console.log(reflect.ownkeys (person)) // delete the property of the object Console. log(Reflect. DeleteProperty (person,'age')) console.log(person) //// PS E:\lagou front-end \demo> node.\ part1\test1.js true [ 'name', 'age' ] true { name: 'yang' }Copy the code

Promise a better asynchronous programming solution

It solves the problem of function nesting too deeply in traditional asynchronous programming

14. The class keyword

14.1 Traditional Use of Prototype

function Person (name) { this.name = name; } Person.prototype.say = function () { console.log(`my name is ${this.name}`) } const p = new Person('tom') p.say() //// PS E:\lagou front-end \demo> node.\part1\test1.js my name is TomCopy the code

14.2 use the class

class Person { constructor(name) { this.name = name } say() { console.log(`my name is ${this.name}`) } } const p = new Person(' Tom ') p.say() //// PS E:\lagou front-end \demo> node.\part1\test1.js My name is TomCopy the code

14.3 Static methods

class Person { constructor(name) { this.name = name } say() { console.log(`my name is ${this.name}`) } static Create (name) {return new Person(name)}} const p = person.create (' Tom ') p.say() //// PS E:\lagou front-end \demo> node .\part1\test1.js my name is tomCopy the code

14.4 inheritance

class Person { constructor(name) { this.name = name } say() { console.log(`my name is ${this.name}`) } static create(name) { return new Person(name) } } class Student extends Person { constructor(name,number) { super(name) this.number = number } hello() { super.say() console.log(`my number is ${this.number}`) } } const s = new Student(' Jack ',100) s.hollo () //// PS E:\lagou front-end \demo> node.\ part1\test1.js My name is jack my number is 100Copy the code

15.Set data type

Const s = new Set() //add returns Set so we can chain s.addd (1).add(2).add(3).add(4).add(4).add(5 console.log(s) //// Set { 1, 2, 3, 4, Log (I => {console.log(I) //// 1 2 3 4 5}) console.log(s.size) //// 5 console.log(s.has(100)) ////false console.log(s.delete(3)) ////true console.log(s) //// Set { 1, 2, 4, 5 }Copy the code

15.1 Array deprocessing by Set

Array.from() converts a Set to an Array

Const result = [1,2,3,1,2,1,4,1] //const result = array. from(new Set(arr)) const result = [...new Set(arr)] Console. log(result) PS E:\lagou front-end \demo> node.\part1\test1.js [1, 2, 3, 4]Copy the code

16.Map data structure

16.1 Common Key-value pairs

The key of an object can only be a String. If the key is not a String, it is automatically converted to a String

const obj = {} obj[true] = 'value' obj[123] = 'value' obj[{ a: 1}] = 'value' console.log(object.keys (obj)) //// PS E:\lagou front-end \demo> node.\part1\test1.js ['123', 'true', '[object Object]' ]Copy the code

16.2 the Map type

Const m = new Map() const Tom = {name:' Tom '} m.net (Tom,90) console.log(m) console.log(m.net (Tom)) //Map traversal M.foreach ((value,key)=>{console.log(value,key)}) //// PS E:\lagou front-end \demo> node.\part1\test1.js Map {{name: 'tom' } => 90 } 90 90 { name: 'tom' }Copy the code

17.Symbol New data type

17.1 The primary function is to add unique attribute names to objects

Const s = Symbol() console.log(s) console.log(typeof s) console.log(Symbol() === Symbol())) Console. log(Symbol('foo')) //// PS F:\ lagou> node.\task1\task1.js Symbol() Symbol false Symbol(foo)Copy the code

17.2 Setting Private Members of an Object

const name = Symbol(); const person = { [name]:'yang', Say (){console.log(this[name])}} console.log(person[Symbol()]) person.say() //// PS F:\ lagou front end \lagou> node .\task1\task1.js undefined yangCopy the code

18. The for of circulation

Is a unified traversal of data

Const arr = [100,200,300,400,500] for (item of arr) {console.log(item) ////100 200 300 400 if(item > 300){break}} The arr.foreach (item =>{console.log(item) ////100 200 300 400}) //some() method is used to check whether the elements in the array satisfy the specified condition (provided by the function). The //some() method executes each element of the array in turn: // If one element meets the criteria, the expression returns true, and the rest of the elements are not checked. // If there is no element that meets the condition, return false. The every() method is used to check whether all elements of an array meet the specified criteria (provided by the function). The //every() method checks all elements in the array using the specified function: // If one element in the array is detected that is not satisfied, the entire expression returns false, and the rest of the elements are not tested. // Return true if all elements meet the criteria. const result = arr.some((item)=>{ return item > 300 }) console.log(result) ////true for (i in arr) { console.log(arr[i]) ////100 200 300 400 500}Copy the code

18.1 Set for of traversal

Const s = new Set(['foo','bar']) for (const item of s) {console.log(item)} //// PS F:\ lagou> node .\task1\task1.js foo barCopy the code

18.2 For of Traversal of Map

const m = new Map() m.set('foo','123') m.set('bar','456') for(const item of m ) { console.log(item) } //// PS F: \ retractor front-end \ lagou > node. \ task1 \ task1 js [' foo ', '123'] [' bar ', '456']Copy the code

18.3 Use array deconstruction to obtain K V

const m = new Map() m.set('foo','123') m.set('bar','456') for(const [key,value] of m ) { console.log(key,value) } //// PS F:\ lagou> node.\task1\task1.js foo 123 bar 456Copy the code

19. Implement Iterable interface

Objects cannot iterate directly

Hangs an iterator method that returns an iterator object

Interface convention: There must be an internal Next method for iteration

const obj = {
    store: ['foo','bar','baz'],
    [Symbol.iterator]: function() {
        let index = 0
        const self = this
        return {
            next: function() {
                const result = {
                    value:self.store[index],
                    done:index >= self.store.length
                }
                index++
                return result
            }
        }
    }
}

for (const item of obj) {
    console.log('循环',item)
}

////
PS F:\拉钩前端\lagou> node .\task1\task1.js
循环 foo
循环 bar
循环 baz
Copy the code

20. Iterator pattern

20.1 Achieve the purpose of iterators

Scenario: Collaborate on a task list application

/ / = = = = = = = = = = = = = = my code = = = = = = = = = = = = = = const todos = {life: [' eating ', 'sleeping', 'play doug'], learn: [' language ', 'mathematics',' English ']. } / / = = = = = = = = = = = = = = your code = = = = = = = = = = = = = = the for (const item of todos. Life) {the console. The log (item)} for (const item of todos.learn){ console.log(item) }Copy the code

If I need to add data to my toDOS data list, your code needs to change accordingly; it’s too coupled for the call

/ / = = = = = = = = = = = = = = my code = = = = = = = = = = = = = = const todos = {life: [' eating ', 'sleeping', 'play doug'], learn: [' language ', 'mathematics',' English ']. Work: [' singing ']} / / = = = = = = = = = = = = = = your code = = = = = = = = = = = = = = the for (const item of todos. Life) {the console. The log (item)} for (const item of todos.learn){ console.log(item) } for(const item of todos.work){ console.log(item) }Copy the code

20.2 How can I Solve the problem

If my code can uniformly provide a traversal interface

For the caller, there is no need to worry about its internal data structure and no need to worry about structural changes

We can write a method to do this ourselves:

/ / = = = = = = = = = = = = = = my code = = = = = = = = = = = = = = const todos = {life: [' eating ', 'sleeping', 'play doug'], learn: [' language ', 'mathematics',' English '], the work: [' sing '], each: function (callback) { const all = [].concat(this.life, this.learn, This. Work) for (item of all) {callback (item)}}} / / = = = = = = = = = = = = = = your code = = = = = = = = = = = = = = todos. Each (function (item) { console.log(item); })Copy the code

20.3 Implemented by iterator method

/ / = = = = = = = = = = = = = = my code = = = = = = = = = = = = = = const todos = {life: [' eating ', 'sleeping', 'play doug'], learn: [' language ', 'mathematics',' English '], the work: [' singing '], [Symbol. The iterator] : the function () {/ / using the object operator is equivalent to [] concat const all = [... this. Life,... this. Learn,... this. Work] let index =  0 return{ next:function(){ return{ value:all[index], done: Index++ > = all. Length / / true will be finished iteration}}}}} / / = = = = = = = = = = = = = = your code = = = = = = = = = = = = = = the for (item of todos) { Console. log(item)} //// PS F:\ lagou> node.\task1\task1.js Eat, sleep, play beans, language, Maths, English, singCopy the code

The core of an iterator is to provide a unified traversal interface so that the external world doesn’t have to worry about what the internal data structure looks like

The each method shown here applies only to the array structure shown here

ES2015 iterators are iterator patterns implemented from the language level, so it is applicable to any data structure, as long as the implementation of the Iterable interface, then we can implement for… Of traverses the object

21. Generator functions

Solve the problem of asynchronous programming callbacks that are too deeply nested

function * foo(){
    console.log('test')
    return 100
}

const result = foo()
console.log(result)
console.log(result.next())

////
Object [Generator] {}
test
{ value: 100, done: true }
Copy the code

21.1 Lazy execution of yield

function * foo(){
    console.log('111')
    yield 100
    console.log('222')
    yield 200
    console.log('333')
    yield 300
}

const generator = foo()
console.log(generator)
console.log(generator.next())
console.log(generator.next())
console.log(generator.next())
console.log(generator.next())

////
Object [Generator] {}
111
{ value: 100, done: false }
222
{ value: 200, done: false }
333
{ value: 300, done: false }
{ value: undefined, done: true }

Copy the code

21.2 the Generator application

Case 1: transmitter

function * createIdMaker(){ let id = 1 while(true) { yield id++ } } const idMaker = createIdMaker() console.log(idMaker.next().value) console.log(idMaker.next().value) console.log(idMaker.next().value) Console. log(idmaker.next ().value) //// PS E:\lagou front-end \demo> node.\part1\test1.js 1 2 3 4Copy the code

Case 2: Implement Iterator methods using Generator functions

Const todos = {life: [' eating ', 'sleeping', 'play doug'], learn: [' language ', 'mathematics',' English '], work: [' singing '], [Symbol. The iterator] : function* () { const all = [...this.life, ...this.learn, ...this.work] for (item of all) { yield item } } } for (const item of todos) { console.log(item) } //// PS E:\lagou front-end \demo> node.\part1\test1.js eat, sleep, play beans, language, Maths, English, singCopy the code

22.ES2016

22.1 Array. Prototype. Includes

Let’s first look at the indexOf method

const arr = ['foo',1,NaN,false] console.log(arr.indexOf('foo')) console.log(arr.indexOf(1)) Console. log(arr.indexof (NaN)) console.log(arr.indexof (false)) //// PS F: lagou> node.\task1\task1.js 0 1-3Copy the code

IndexOf cannot check for NaN

NaN is a special numeric value (the result of typeof NaN is number), which is short for not a number, indicating that it is not a valid number.

Note: NaN is the only value not equal to itself: NaN === NaN // false

22.2 includes checks that NaN returns a Boolean value

const arr = ['foo',1,NaN,false] console.log(arr.includes('foo')) console.log(arr.includes(1)) Console. log(arr.includes(NaN)) console.log(arr.includes(false)) //// PS F: lagou> node.\task1\task1.js true true true trueCopy the code

22.3 Exponential operators

Console. log(math.pow (2,10)) console.log(2 ** 10) //// PS F:\ lagou> node.\task1\task1.js 1024 1024Copy the code

23.ES2017

23.1 the Object values

const obj = { foo: 'value1', bar: 'balue2'} console.log(object.values (obj)) //// PS F:\ lagou> node.\task1\task1.js ['value1', 'balue2']Copy the code

23.2 Object. Entries

Array returns key-value pairs

const obj = { foo: 'value1', bar: 'balue2'} console.log(object. entries(obj)) //// PS F:\ lagou> node.\task1\task1.js [['foo', 'value1'], [ 'bar', 'balue2' ] ]Copy the code

So you can use “for of.

const obj = { foo: 'value1', bar: 'balue2' } for (const [key, value] of Object.entries(obj)) { console.log(key, Value)} //// PS F:\ lagou> node.\task1\task1.js foo value1 bar balue2Copy the code

Convert Map objects using entries

const obj = { foo: 'value1', bar: 'balue2'} console.log(new Map(object.entries (obj))) //// PS F:\ lagou> node.\task1\task1.js Map {'foo' => 'value1', 'bar' => 'balue2' }Copy the code