• An object is an abstraction of a single thing
  • An object is a container that encapsulates properties and methods to represent the state of the object, while methods represent the behavior of the object

What is object orientation

Object Oriented Programming, OOP for short

Object orientation is the encapsulation of process orientation

Object-oriented characteristics:

  • Encapsulation; Inheritance; polymorphism

Object oriented design ideas

  • Abstract out the Class(constructor), containing properties and methods
  • Create Instance from Class(constructor)
  • Command the result of Instance

Several ways to create objects

  • The new Object() constructor is called, and the properties are assigned
  • Object literals {}, object assignment directly simplifies the code
  • Encapsulating factory functions encapsulates a method of a new Object() or literal into a factory function and returns the result. The factory function can be called many times later, but the Object generated by the factory function is not a concrete instance
  • Custom constructor #5

    Differences with factory functions:
    • Don’t need to return
    • The function name of a constructor must be capitalized
    • No object is generated internally. Use this to refer to the object to be generated
    • After the constructor is defined, use new to generate the instance

      The new keyword does:
      1. Create a new object
      2. This points inside the function to the new object
      3. Execute the code inside the constructor
      4. (Default) the new object is returned

    Instances generated from constructors have the constructor attribute, which points to the instance’s constructor, but it may be modified. The instance of operator is used when determining the relationship between instances and constructors.

Static and instance members #

When you create an object using a constructor method, you can add properties and methods, called members, to the constructor and to the instance object you create.

  • Static member: A member added to the constructor itself. Can only be called using constructors, not generated instance objects.
  • Instance member: the member added to this inside the constructor. It must be called by the object after the instance object is created, not by the constructor.

All members of Math are static because it is not generated by the constructor.

Constructor problem

Wasted memory: The functions of each instance are optimized differently: the public functions are extracted outside the constructor, and the function attributes inside the constructor are assigned to go further: multiple public functions are encapsulated as a single object, avoiding the global scope of numerous function declarations

Prototype object #8

Any function has a prototype property whose value is an object, called a prototype object. Properties and methods can be added to the prototype object. By default, the constructor prototype object has a constructor property pointing to the constructor itself. Person === Person.prototype.constructor;

The instance contains a __proto__ attribute that points to the prototype object of its constructor. Person.prototype === p1.__proto__ This pointer is nonstandard and can be omitted in practice, i.e. instances can directly access members of the prototype object, such as constructor. As mentioned in the previous section, the instance calls constructor to point to its constructor.

The optimal solution # 9

Public functions do not need to be wrapped separately into the object, but are added directly to the constructor’s prototype object. The properties and methods of the prototype object are inherited by the constructor and by the instance generated by the constructor.

The prototype chain # 10

The instance object reads and writes the prototype object member #11

Read: Prototype chain lookup

  • Look for yourself first, and return when you find it
  • If you can’t find it on your own body, you’ll look up the prototype chain and return if you find it
  • If the end of the prototype chain (NULL) is not found, undefine is returned

Write: divided into two categories of cases

  1. Value type members write to (instance objects. Member = xx) e.g.s1. Age =18; : Reference type member writes (instance object. Prototype reference type member = xx) e.g.ps1.type=’person’; :

    Writes or modifications only apply to the instance itself, masking access to the members of the prototype object and not looking up the prototype chain.

  2. Complex type member modification (instance object. Member object. Xx = xx) :

    It looks up the prototype chain

    • It will also look for the member on its own body first, and if it is found on its own body, it will directly modify
    • If you can’t find it on your own, continue to look along the prototype chain, and if you find it, modify it
    • If the member is not found until the end of the prototype chain, an error is reported (instance object.undefined. Xx = xx)

Simpler prototype syntax #12

You can reset a prototype object to a new object directly as an object literal.

The constructor property of the original prototype object will be overwritten and lost. Add constructor manually when assigning the object and point it to the correct constructor.

Class of members when defining constructors:

  • Private members: Generally non-function members that are put directly into the constructor definition
  • Shared members: Usually functions, put into the prototype object
  • Special member: Need to fix if you reset Prototype

The prototype object of the native constructor

The built-in native constructors are Object(), Function(), Array(), String(), and Number(), whose prototype objects cannot be overwritten with literals, but can be written with new attributes

【 Case 】 Random blocks