Object oriented JS

Time:2021-1-10
Object oriented language is to master the knowledge of class, object and instance and their relationship. Such as encapsulation, inheritance and polymorphism of ‘class’.
  • Object: all things are objects, and all kinds of things can be our research objects.
  • Class: classify, extract and encapsulate the common parts of objects with the same attributes. For example, in the category of human, gender is one of the attributes of human.
  • Instance: derived from a class is called an instance. Instances have both private properties and public properties inherited from classes.
Encapsulation, inheritance and polymorphism of classes in JS
  • The implementation of class in JS is realized by prototype and prototype chain
    1.1, prototype: the value of each function data type contains the prototype attribute (this attribute comes naturally). This property value is an object used to store the common properties and methods of the instance.
    1.2, in the prototype attribute object, there is a constructor attribute (which is also born with), which points to the function itself. (similar to C + + constructor)
    1.3. The value of each object data type contains the privacy prototype attribute__ proto__ , which points to the prototype of the class to which the object belongs
    2.1, prototype chain: each object contains a privacy prototype__ proto__ And points to the prototype of the class to which it belongs, so each object will be connected to its superior through the prototype attribute. We call this method of connecting through the prototype attribute prototype chain.
    2.2, the search mechanism of prototype chain: first search on your own private attribute, and pass if you don’t find it__ proto__ This property looks up the prototype of the class to which it belongs. If it does not pass the prototype’s__ proto__ Keep looking up until you find the base class Object.prototype until. (object is the base class, so its__ proto__ Is null).
    3.1, the search mechanism of this prototype chain is the implementation mechanism of class inheritance and polymorphism in JS, as follows:
    Example 1:

    function Person(name, age){
        this.name = name
        this.age = age
    }
    Person.prototype.speak = function(){
      console.log('i can speak chinese') 
    }
    Let P1 = new person ('xiao Ming ', 18)
    p1.speak = function(){
      console.log('i can speak english') 
    }
    console.log(p1.speak()) //  'i can speak english'
    Let P2 = new person ('xiao Hong ', 22)
    console.log(p2.speak()) //  'i can speak chinese'

    Example 2:
    Object oriented JS

  • Hasownproperty can be used to check whether a property is private to the object
let ary = [1,2,3]
console.log ( arr.hasOwnProperty ('Push ') // false because' push 'is a public attribute of the array class, not a private attribute of the current instance' array '
  • Through ‘in’, you can check whether a property is a property of the object (including public and private properties)
let ary = [1,2,3]
console.log('push' in ary)// true