3 ways to create JavaScript objects

3 ways to create JavaScript objects

Preface:

In JavaScript , an object is an unordered collection of property names and property values. Objects can be created using object literals, the new keyword, and Object.create() function.

1. Object literals

let obj = {} // empty object let obj2 = {a:1, b:2}

let obj3 = {" hel": "wold"} // If the property name has spaces, you can use a string literal as the property name

2. The new keyword creates an object

Use the new keyword followed by a constructor to create a new object

let o = new Object(); // built-in constructor let m = new Math();

let a = new Array();

let d = new Date();

function Person(){ //Custom constructor}
let person = new Person()


3. Create an object using Object.create()

let o = Object.create({x:1, y:2});
console.log(o.x+oy) //3


The new object o will inherit {x:1, y:2} . The properties x and y are called inherited properties. If the passed parameter is null , this object will not inherit any object. The inherited object is also called the "prototype".

Object.create(null)

4. Use the extension operator:...

ES2018 adds the spread operator ... to copy existing object properties to a new object

let foo = {x:1, y:2}
let bar = {z:3}

let zoo = { ...foo, ...bar}

console.log(zoo) // {x:1, y:2, z:3}


A few points to note:

  • The extension operator only extends the object's own properties. Inherited properties do not support extension.
  • If the extending object and the extended object have properties with the same name, the value of the property is determined by the latter object.

5. Use Object.assign() method

assign can copy the properties of one object to another object. assign accepts two or more parameters. The first parameter is the target object, and the second and subsequent parameters are the source objects. The function will copy the properties of the source object to the target object and return the target object.

let foo = {x:1, y:2}
let bar = {z:3}

let zoo = {}

let obj = Object.assign(zoo, foo, bar)

console.log(zoo) // {x:1, y:2, z:3}

console.log(obj===zoo) // true  

In addition, two new object features added in ES6

6. Abbreviated properties

If you want to create an object composed of multiple variable names and corresponding values, you need to construct the object like the traditional object literal syntax.

let x = 1, y = 2;
let o = {x:x, y:y}

console.log(o) // {x:1, y:2}


After ES6 , you can directly abbreviate properties, omitting semicolons and property names

let o2 = {x, y}

console.log(o2) // {x:1, y:2}

7. Abbreviation method

When defining methods in an object, ES6 need to define them through function expressions, just like defining ordinary properties.

let point = {
    x:1,
    y:2,

    area: function(){
        return this.x*this.y
    }
}

console.log(point.area()) //2

After ES6 , the colon and function keyword can be omitted to define object methods in a simple way.

let point2={
    x:1,
    y:2,

    area(){
        return this.x*this.y
    }
}

console.log(point2.area()) //2

This concludes this article about 3 methods of creating JavaScript objects. For more information about JavaScript object creation methods, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of various ways to create objects in js and their advantages and disadvantages
  • Detailed explanation of several ways to create objects and object methods in js
  • Summary of six ways to create custom objects in JS
  • Detailed explanation of javascript object creation mode
  • Detailed explanation of JavaScript object-oriented programming [class creation, instance objects, constructors, prototypes, etc.]
  • Simple example of how to create immutable objects in native JavaScript
  • Summary of JS object creation pattern examples
  • JS custom object creation and simple usage examples
  • Analysis of four common patterns for creating objects in JavaScript

<<:  Introduction to the use of MySQL official performance testing tool mysqlslap

>>:  CSS uses radial-gradient to implement coupon styles

Recommend

Creating Responsive Emails with Vue.js and MJML

MJML is a modern email tool that enables develope...

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

Box-shadow and drop-shadow to achieve irregular projection example code

When we want to add a shadow to a rectangle or ot...

Steps to deploy ingress-nginx on k8s

Table of contents Preface 1. Deployment and Confi...

HTML+CSS to achieve drop-down menu

1. Drop-down list example The code is as follows:...

HTML page header code is completely clear

All the following codes are between <head>.....

How to implement a binary search tree using JavaScript

One of the most commonly used and discussed data ...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

Detailed explanation of CSS counter related attributes learning

The CSS counter attribute is supported by almost ...

Summary of Mysql slow query operations

Mysql slow query explanation The MySQL slow query...

Linux uses stty to display and modify terminal line settings

Sttty is a common command for changing and printi...