Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ES6 object

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report