About the understanding of objects, what is object-oriented, the understanding of objects in JavaScript

What is Object Oriented?

Object-oriented is an important programming idea, which abstracts preprocessing problems into objects to solve practical problems.

Object-oriented three characteristics:

  • Encapsulation (core): Encapsulate the properties and methods of the object, and its carrier is an object or class. After encapsulation, the internal implementation details are usually hidden, and you only need to use its interface (method)

  • Inheritance: the hierarchical relationship between classes, the subclass can inherit all the properties and methods of the parent class, and the subclass can extend what the parent class does not have

  • Polymorphism: the execution results of the same method in different objects are not the same (one interface, multiple methods)

class-object relationship

Class: It is a collection of objects with the same properties and methods (a class is a template of an object)
Object: Any objective thing or entity is an object, and an object is a collection of properties and methods (an object is an instance of a class)

in JavaScript_

  • Any objective thing or entity is an object (an object is a collection of properties and methods)
  • The class is ES6officially introduced, and object-oriented programming needs to be implemented through constructors and prototype objects
  • Everything in js is an object: numbers, strings, arrays, etc.
  • js objects are containers for variables, methods, and properties (objects are also variables)

​Create an object and assign values, access properties and methods (when a function is assigned to an object's properties, it is called a "method")

//一个值
var people = {
    
     name:"zhangsan" };

//多个值以键值对形式,逗号分隔
var people = {
    
    
    name:"zhangsan",
    age:18,
    sex:"男",
    fun : function(){
    
    
       console.log(this.name); //这里的this指向的是people这个对象
    }//fun就成了该对象的一个方法
};

//.或[]方式访问属性
people.name或people['name']
//obj.属性() 方式访问对象方法
people.fun(); //zhangsan

Guess you like

Origin blog.csdn.net/wuyou_w/article/details/127847240