The relationship between JS constructor and instantiation and prototype introduction

The relationship between JS constructor and instantiation and prototype introduction

1. Constructor and instantiation

When our programming is object-oriented, the first process is abstraction => then instantiation. For example, we abstract a person and I know the basic information of the person. Name, age, gender, etc.

We abstract first, and after the abstraction is completed, we instantiate.

2. What is the relationship between constructor and instantiation?

//This custom constructor is in the abstract function Person(name,age,sex){

    this.name=name;

    this.age=age;

    this.sex=sex;

    this.say=function(){

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

    }

}

// This process is instantiation let per1=new Person('司藤',300,'女');

per1.say(); //Call //let per1=new Person('司藤',300,'女');

Through the above line of code.

We can draw a conclusion:

The relationship between the constructor and the instance object is:

Instance objects need to be created through constructors.

At the same time: we can know that the constructor of the instance object is the constructor

Let's prove this statement is correct; the above code does not change.

console.log( per1.constructor===Person ) //Returns true

Fully explain: The statement that the constructor of an instance object is the constructor is correct.

3. Is per1.say equal to per2.say?

 function Person(name,age,like) {

    this.name=name;

    this.age=age;

    this.like=like;

    this.say=function(){

        console.log('I can skip meals');

    }

}

var per1=new Person("司藤",300,'玩');

var per2=new Person('白浅','10000','玩');

per1.say();

per2.say();

console.log( per1.say == per2.say ) //false

4. per1.say is not equal to the conclusion drawn by per2.say

Because console.log( per1.say == per2.say ) //false

We can draw a conclusion.

That is, per1.say() and per2.say() do not call the same method.

Are their contents equal?

console.log( per1.say()==per2.say() ) //true

The description content is equal

5. Example code problem

5.1 Problems with the code

function Person(name,age,like) {

    this.name=name;

    this.age=age;

    this.like=like;

    this.say=function(){

        console.log('I can skip meals');

    }

};

for (var index = 0; index < 100; index++) {

    var per=new Person("司藤",300,'玩');

    per.say();

}

This piece of code: It opens up 100 spaces in memory. Each space has a say method. But each say method is different. But their output is the same. In other words, the execution logic is the same. This results in a waste of space. So in the project, this results in a waste of space.

Can we optimize it?

5.2 Optimize code to solve space waste

function comSay(){

    // Execute the same logic console.log('I can skip meals')

};



function Person(name,age,like) {

    this.name=name;

    this.age=age;

    this.like=like;

    this.say=comSay; //Do not add brackets};

var per1=new Person("司藤",300,'玩');

var per2=new Person('白浅','10000','玩');

console.log( per1.say==per2.say ) //true

This way we save space. Each time it is called, it is the same method.

5.3 Using this method, we can also use the prototype method

function Person(name,age,like) {

    this.name=name;

    this.age=age;

    this.like=like;

};

Person.prototype.comSay=function(){

    console.log('I can skip meals')

}

var per1=new Person("司藤",300,'玩');

var per2=new Person('白浅','10000','玩');

console.log( per1.comSay==per2.comSay ) //true

// We can also solve data sharing through prototypes

The role of the prototype: data sharing and space saving.

This concludes this article on the relationship between JS constructors and instantiations and the introduction of prototypes. For more information on the relationship between JS constructors and instantiations and the introduction of prototypes, please search 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:
  • Detailed explanation of JavaScript prototype and examples
  • An article to help you understand Js inheritance and prototype chain
  • Summary and practice of javascript prototype chain diagram
  • Handwriting implementation of new in JS
  • Detailed description of the function of new in JS
  • How much do you know about JavaScript's constructor, prototype, prototype chain and new?

<<:  Introduction to the use of this in HTML tags

>>:  Detailed examples of how to use the box-shadow property in CSS3

Recommend

Use of Linux ipcs command

1. Command Introduction The ipcs command is used ...

HTML 5.1 learning: 14 new features and application examples

Preface As we all know, HTML5 belongs to the Worl...

CSS realizes the realization of background image screen adaptation

When making a homepage such as a login page, you ...

Detailed explanation of mysql replication tool based on python

Table of contents 1. Introduction Second practice...

Solution to the IP address not being displayed under Linux

Table of contents Preface Solution: Step 1 Step 2...

Application of mapState idea in vuex

Table of contents 1. Map method 2. Application ba...

What are the new features of Apache Spark 2.4, which will be released in 2018?

This article is from the Apache Spark Meetup held...

Java example code to generate random characters

Sample code: import java.util.Random; import java...

How to make spaces have the same width in IE and FF?

body{font-size:12px; font-family:"宋体";}...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

Detailed explanation of the steps to create a web server with node.js

Preface It is very simple to create a server in n...

Linux system dual network card binding configuration implementation

System version [root@ ~]# cat /etc/redhat-release...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...