1. JS itself is based on object-oriented thought developed out of the programming language, so learning and developing JS, but also according to the object-oriented thought to deal with!

  • Built-in class

    • Each data type has its own class

      • Number: Number class, of which every NaN and Infinity is an instance

      • String

      • Boolean

      • Symbol

      • BigInt

      • Array

      • RegExp

      • Date

      • Function

      • Object

      • .

    • Each DOM element also has its own class

      • document -> HTMLDocument -> Document -> Node -> EventTarget -> Object

      • .

  • Custom class “Create a function fn”

    • The fn() ordinary function performs the “stack mechanism”

    • The new fn() constructor performs “stack + object-oriented”

      • "Similar"

        • The same is true for executing the function (the same is true for passing arguments)

        • Forming a private context

        • There are private variables

        • .

      • "Different"

        • New, the browser creates an object (instance object) in the current context by default

        • When we initialize this, we make this point to the instance object. This. XXX = XXX is written in the code to set the private property of the instance object

        • If a function returns no value or a primitive type value, the created instance object is returned by default. If the value you return is a reference type, the value you return is preferred.

        • New can be executed without parentheses if the class does not need to pass arguments. Set the parentheses, called the argument list new; In addition to whether the argument is passed, there are also differences in the priority of the operation.

    function Fn(x, y) {
        let total = x + y
        this.x = x
        this.y = y
    }
    const fn = new Fn(1.2)
    Copy the code