Detailed explanation of JS instantiated objects

First understand three concepts:

  1. kind

  1. instance object (object)

  1. Instantiate

a. Category : Literally understood, such as "human beings" - everyone belongs to human beings, but "human beings" is not a human being but a general term for species (people) with multiple identical attributes, both men and women. Properties in "human". " The concept of class is not concrete but abstract!"

b. Instance object (object) : The specific thing is the object. For example: You, who also belong to "human beings", are a specific object among humans. You cannot instruct "human beings" to write code because it is just a concept, but you can write code because you are a specific object among "human beings". Instance, you are not conceptual but material. " The specific individual is the object! "

c. Instantiation : When the boss asks you to write code, assign this specific task to you. This process is instantiated into you in "human beings". This process is called instantiation. The process of creating specific objects using classes is instantiation!

In programming languages, "class" has many methods and attributes. However, since "class" is an abstract concept, there is no way to directly use the methods and attributes. The only way is to instantiate this "class" into a specific "instance". Only "object" can call methods and attributes belonging to "class". "Instance object" has all the attributes and methods of "class". Only when abstract concepts are concreted into an individual can they be used better. The process from class concrete to instantiated object and from abstract to concrete is called instantiation.

Code analysis:

//创建一个类。类命名为Peopel
class People {
    //定义了一个函数方法Man()
     Man() {
    return "嘿man!来活了!!!"
  }
}
//new People()这就是一个标准的构造函数
//声明了实例化一个对象work
//用构造函数new People()初始化work并赋值
//与'new'运算符一起使用,用来创建对象并初始化对象的'函数'就是构造函数
const work = new People()
//现在就可以使用类中的方法和属性了
console.log(work.Man())//嘿man!来活了!!!
console.log(work)//

Screenshot of code running:

Extensions:

constructor() method: It is the default method of the class. This method is automatically called when an object instance is generated through the new command. A class must have a constructor() method. If not explicitly defined, an empty constructor() method will be added by default.

Guess you like

Origin blog.csdn.net/weixin_51828648/article/details/128875004