Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the ES6 object

Shulou Source: shulou.com Published: 2022-05-31 18:30:03 09月30日 Update

This article introduces the knowledge of "how to use ES6 objects". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. objects and properties and methods

Objects in JavaScript:

Var person= {name: "Jack", age:20}

Or:

Var name = "jack"; var age = 20 X var person = {name:name,age:age}; console.log (person.age); / / 20

The concise expression in ES6:

Let [name,age] = ["jack", 20]; let person = {name,age}; / / equivalent to person = {name: name,age: age} console.log (person.age); / / 20

Object methods in JavaScript:

Var person = {SayHi:function () {console.log ("hi");}} person.SayHi (); / / hi

It is succinctly stated in ES6:

Let person = {SayHi () {console.log ("hi");}} person.SayHi ()

ES6 allows expressions as attribute names, but be sure to put expressions in square brackets:

Var person = {["na" + "me"]: "jack", ["a" + "ge"]: 20}; console.log (person.age)

Or

Let str = "Hi"; let person = {["Say" + str] () {console.log ("hi");}} person.SayHi ()

Note: the concise representation of the attribute and the expression of the attribute name cannot be used at the same time, otherwise an error will be reported:

Let [name,age] = ["jack", 20]; let person = {["na" + "me"], ["a" + "ge"]}; / / error II, object extension operator.

Take out all traverable properties of the parameter object and copy them to the current object:

Let stu1 = {name: "Jack", age:20,sex: "male"}; let stu2 = {... stu1}; console.log (stu2); / / {name: "Jack", age:20,sex: "male"}

Merge two objects:

Let stu1BasicInfo = {name: "Jack", age:20,sex: "male"}; let stu1DetailInfo = {phone: "13524521457", mail: "ldh@163.com"}; let stu = {... stu1BasicInfo,...stu1DetailInfo}; console.log (stu); / / {name: "Jack", age:20,sex: "male", phone: "13524521457", mail: "ldh@163.com"}

Adding attributes is supported when copying objects:

Let stu1 = {name: "Jack", age:20,sex: "male"}; let stu2 = {... stu1,phone: "13554785452"}; console.log (stu2); / / {name: "Jack", age:20,sex: "male", phone: "13554785452"}

The added attribute is after the extension operator, take the added attribute:

Let stu1 = {name: "Jack", age:20,sex: "male"}; let stu2 = {... stu1,name: "rose", phone: "13554785452"}; console.log (stu2); / / {name: "rose", age:20,sex: "male", phone: "13554785452"}

Before the extension operation, the added attribute takes the content of the extension operator:

Let stu1 = {name: "Jack", age:20,sex: "male"}; let stu2 = {name: "rose", phone: "13554785452",... stu1}; console.log (stu2); / / {name: "Jack", phone: "13554785452", age:20,sex: "male"} "how to use ES6 objects", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Objects attributes operations simplicity content expressions operators copies methods more knowledge practicality learning and then two parameters at the same time dilemma actual situation. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB Xiaomi Linux Microsoft