PrefaceLike other programming languages, JavaScript has its own data types such as numbers, strings, arrays, objects, etc. Objects are a very important data type in JavaScript. They have many useful methods that can be used to easily handle objects in daily project development. This article introduces 6 methods that can be used in projects. Take this opportunity to deepen your understanding of their usage. 1. Object.freeze() const author = { name: "Quintion", city: "Shenzhen", age: 18, validation: true, }; Object.freeze(author); author.name = "QuintionTang"; author.province = "Guangdong"; delete author.age; console.log(author); // { name: 'Quintion', city: 'Shenzhen', age: 18, validation: true } As shown in the code above, the attribute 2. Object.seal() const author = { name: "Quintion", city: "Shenzhen", age: 18, validation: true, }; Object.seal(author); author.name = "QuintionTang"; author.province = "Guangdong"; delete author.age; console.log(author); // { name: 'QuintionTang', city: 'Shenzhen', age: 18, validation: true } As you can see from the above code, adding new attributes and deleting attributes are invalid, only updating the attribute 3. Object.keys() Take a look at the following code: const author = { name: "Quintion", city: "Shenzhen", age: 18, validation: true, }; console.log(Object.keys(author)); // [ 'name', 'city', 'age', 'validation' ] You can see that the result printed in the above code is an array containing the keys as output. The output results can be processed or iterated using array methods. console.log(Object.keys(author).length); // 4 4. Object.values() const author = { name: "Quintion", city: "Shenzhen", age: 18, validation: true, }; console.log(Object.values(author)); // [ 'Quintion', 'Shenzhen', 18, true ] 5. Object.create() const author = { firstName: "Quintion", lastName: "Tang", fullName() { return `${this.firstName} ${this.lastName}`; }, }; const newAuthor = Object.create(author); console.log(newAuthor); // {} newAuthor.firstName = "Ronb"; newAuthor.lastName = "Joy"; console.log(newAuthor.fullName()); // Ronb Joy In the above code, 6. Object.entries() Object.entries() allows you to get the keys and values of an object, returning a multidimensional array where each dimension contains each key and value, such as const author = { firstName: "Quintion", lastName: "Tang", fullName() { return `${this.firstName} ${this.lastName}`; }, }; console.log(Object.entries(author)); The output is as follows: [ [ 'firstName', 'Quintion' ], [ 'lastName', 'Tang' ], [ 'fullName', [Function: fullName] ] ] SummarizeThis article briefly introduces the six common methods of objects and provides corresponding sample codes. In the actual process of coding and processing objects, using the above methods can make the code more elegant. This concludes this article about 6 ways to elegantly process objects in JavaScript. For more content related to JavaScript processing objects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The difference and usage of single-line and double-line layout in Flex mobile layout
>>: Solution to the problem that MySQL commands cannot be entered in Chinese
At present, most people who use Linux either use ...
Table of contents 1. Introduction 2. Output Infor...
TypeScript Bundling webpack integration Usually, ...
Table of contents TypeScript environment construc...
Table of contents 01 Common Faults 1 02 Common Fa...
1. Install Docker on the server yum install docke...
The nginx configuration is as follows: Such as ht...
A mature database architecture is not designed wi...
Effect: Code: <template> <div class=&quo...
Page Description: Main page: name —> shisheng...
Table of contents 1. Data Type 1. What is MySQL s...
Table of contents 1. Character Function 1. Case c...
1. Introduction The main value that SELinux bring...
If the words in the sql statement conflict with t...
0x00 Introduction WordPress is the most popular C...