Four ways to create objects in JS

Four ways to create objects in JS

There are 4 ways to create objects:

  • 1. Create objects using literal values
  • 2. Use the new character to create an object
  • 3. Custom constructor
  • 4. Factory pattern creates objects

An object has properties and methods.

1. Create objects by literal value

var person1 = {

    name:'Conan',

    age:12,

    sex:'male',

    eat:function(){

        console.log("I'm hungry and want to eat")

    },

    say:function(){

        console.log("My name is Conan")

    }

}

console.log("person.eat output function",person.eat)

person.eat(); //Directly output I am hungry and want to eat

What we need to pay attention to is: the difference between person.eat and person.eat()

  • person.eat is the output function
  • person.eat() is the calling function

2. Use the new character to create an object

var person2 = new Object();

person2.name = 'Conan'

person2.age = '21'

person2.sex = 'male'

person2.eat=function(){

    console.log("I'm hungry and want to eat")

}

person2.say=function(){

    console.log("My name is Conan")

}

console.log('sex',person2.sex) //output male

The disadvantages of the above two ways to create objects:

Although both of the above can create objects

But we don’t know what type the object we created is?

Of course we know they are of type OBject

At this time we need to use a custom constructor to create an object

3. Create an object with your own constructor

function Person(name,age,sex,like){

    console.log('No value is passed to the like parameter, it is undeined',like)

    // Here this refers to the Person object console.log('this',this)

    this.name=name;

    this.age=age;

    this.sex=sex;

    this.say=function(){

        console.log("My name is",name)

    }

}

//This line of code means creating an object//Instantiating an object at the same time//And initializing the properties of this object//So this line of code is not simple let per1=new Person('Conan',19,'男');

per1.say();

console.log(per1 instanceof Person);//true

Now we know that per1 is of type Person . This is the advantage of creating objects with custom constructors. We know what type of object it creates.

ps: Constructors usually start with a capital letter


When we create a new object, we do four things:

By creating a custom object, we understand that when we create a new object, we do four things:

  • 1. Create space to store the current object
  • 2. Set this to the current object
  • 3. Set properties and methods
  • 4. Return this object

4. Factory pattern creates objects

function createObj(name,age) {

    let obj = new Object();

    obj.name=name;

    obj.age=age;

    obj.sayHi=function(){

        console.log(obj.name)

    }

    return obj;

}

let per=createObj('司藤',200)

console.log(per.age); //200

per.sayHi(); //Si Teng

This concludes this article about the four ways to create objects in JS. For more information about the four ways to create objects in JS, 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:
  • 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 JavaScript object-oriented object creation method
  • How to create functions and objects in JS
  • Summary of JavaScript object creation methods [factory pattern, constructor pattern, prototype pattern, etc.]
  • Summary of common ways to create objects in JavaScript
  • Summary of common ways to create custom objects in JavaScript
  • A comprehensive summary of seven ways to create objects in JavaScript
  • Analysis of common methods and principles of creating JS objects
  • Seven ways to create objects in JavaScript (recommended)

<<:  Detailed explanation of the alternative implementation code of CSS vertical centering (unconventional)

>>:  Some thoughts and experience sharing on web page (website) design and production

Recommend

MySQL index leftmost principle example code

Preface I was recently reading about MySQL indexe...

HTML table tag tutorial (23): row border color attribute BORDERCOLORDARK

In rows, dark border colors can be defined indivi...

Refs and Ref Details in Vue3

The editor also shares with you the corresponding...

Detailed installation tutorial of Mysql5.7.19 under Centos7

1. Download Download mysql-5.7.19-linux-glibc2.12...

Analysis of the Docker deployment Consul configuration process

Execute Command docker run -d --name consul -p 85...

Using JS to determine the existence of elements in an array in ten minutes

Preface In front-end development, you often need ...

MySQL's method of dealing with duplicate data (preventing and deleting)

Some MySQL tables may contain duplicate records. ...

Detailed explanation of HTML onfocus gain focus and onblur lose focus events

HTML onfocus Event Attributes Definition and Usag...

mysql8.0.11 winx64 manual installation and configuration tutorial

First of all, let me talk to you about my daily l...

A simple and in-depth study of async and await in JavaScript

Table of contents 1. Introduction 2. Detailed exp...

MySQL 8.0.23 installation super detailed tutorial

Table of contents Preface 1. Download MySQL from ...

js implements array flattening

Table of contents How to flatten an array 1. Usin...

How to use html2canvas to convert HTML code into images

Convert code to image using html2canvas is a very...

JavaScript drag time drag case detailed explanation

Table of contents DragEvent Interface DataTransfe...