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

What is the method of manipulating objects in JavaScript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about the method of operating objects in JavaScript, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Object.create ()

Create a new object instance based on the parameter.

Const user = {name: 'kylin', age: 18, gender:' female', work: 'dev', say () {console.log (`name: ${this.name}, age: ${this.age}, gender: ${this.gender} `);}}; const my = Object.create (user); my.say (); / / name: kylin, age: 18, sex: female my.name =' Tom' My.year = '2020; console.log (my.name); / / Tom console.log (my.year); / / 2020

Object.assign ()

Merge (copy) all enumerable properties from one or more objects to a new object.

The Object.assign () method has two parameters, the first is the target object, and the second is the source object. If there is an attribute of the same name, the property of the later object overrides the property of the previous object.

Const userBasic = {name: 'kylin', age: 20,}; const userInfo = {age: 18, gender:' female', work: 'dev', say () {console.log (`name: ${this.name}, age: ${this.age}, gender: ${this.gender}, job: ${this.work} `);}}; const user = Object.assign (userInfo, userBasic); user.say () / / name: kylin, age: 20, gender: female, job: dev

You can also merge into a new object.

Const userBasic = {name: 'kylin', age: 18, gender:' female', say () {console.log (`name: ${this.name}, age: ${this.age}, gender: ${this.gender} `);}}; const user = Object.assign ({}, userBasic); user.say (); / / name: kylin, age: 18, sex: female

Object.keys ()

Returns an array of all properties that can be enumerated by the object itself.

Const user = {name: 'kylin', age: 18}; Object.defineProperty (user,' gerder', {value: "female", enumerable: false / / not enumerable}); console.log (Object.keys (user)); / / ["name", "age"]

Object.values ()

Returns an array of values of all the enumerable properties of the object itself, in the same order as using the for...in loop.

Const user = {name: 'kylin', age: 18}; Object.defineProperty (user,' gerder', {value: "female", enumerable: false / / non-enumerable}); console.log (Object.values (user)); / / ["kylin", 18]

Object.entries ()

The properties and values (keys and values) of the object are represented as an array. That is, an array of key-value pairs containing the enumerable properties of the object itself is returned.

Const user = {name: 'kylin', age: 18}; const info = Object.entries (user); console.log (info); / / [[' name', 'kylin'], [' age', 18]]

Object.fromEntries ()

This is equivalent to the inverse operation of the Object.entries () method, which converts an array in the form of key-value pairs into objects.

Const user = Object.fromEntries ([['name',' kylin'], ['age', 18]]); console.log (user); / / {name: "kylin", age: 18} these are the methods of manipulating objects in JavaScript. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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