Skip to main content

Inheritance of classes in ES6 and the prototype relationship between classes

 JavaScript does not have the concept of classes in languages such as JAVA in the strict sense before ES6. ES6 added class, but in fact this class is just syntactic sugar for constructors and prototypes before ES6. To really understand the most complex parts of JavaScript, you have to start with the initial constructors and prototypes. Prototype is one of the characteristics of JavaScript. Although the object-oriented concepts such as classes and instances are similar to languages such as Java, they are not the same in essence. In JavaScript, each object instantiated by the constructor generally only contains its own properties, but when we call a method or property that is not on the object, the instantiated object will look up its own prototype. . If the prototype has not been found, and the prototype object has its own prototype, it will go back to the top prototype along the prototype chain.

First define two classes

        class A{
            static s="AAA";
            s1="aaaa";
            constructor(id,name){
                this.id=id;
                this.name=name;
            }
            funA(){
                console.log("FUNA")
            }
        }
        //
        class B extends A{
            static c="BBB";
            c1="bbb";
            constructor(id,name,age){
                super(id,name);
                this.age=age;
            }
            funB(){
                console.log("FUNB");
            }
        }

The Object.getPrototypeOf method can be used to obtain the parent class from the subclass. This method can be used to determine whether a class inherits from another class.

    console.log(Object.getPrototypeOf(B) === A)// true

Instantiate two objects and see their prototype relationship.

        let a=new A(2001,"a01");
        let b=new B(1001,"b01",100);
        console.log(a.__proto__===B.prototype.__proto__);//true
        console.log(A.prototype===b.__proto__.__proto__);//true
        console.log(B.prototype===a);//false
        // console.dir(A);
        // console.dir(B);
        console.log(A===B.__proto__);//true
        console.log(a.__proto__.constructor===B.__proto__);//true

Define a method to compare whether the addresses of two objects are equal, and perform address comparison

        function isTwoObjEqual(obj1,obj2){
            let o1=Object.getOwnPropertyNames(obj1);
            let o2=Object.getOwnPropertyNames(obj2);
            if(o1.length!=o2.length){
                return false;
            }
            let maxO1Length=o1.length;
            for(let i=0;i<maxO1Length;i++){
                let strName=o1[i];
                if(o1[strName]!==o2[strName]){
                    return false;
                }
            }
            return true;
        }
        console.log(isTwoObjEqual(B.__proto__.prototype,a.__proto__),"---");//true
        let result=isTwoObjEqual(A.prototype,b.__proto__.__proto__);//true
        console.log(result);//true
        console.log(b);
        console.log(b.age,b.c1,b.s,b.s1,b.c,B.c,B.s)
                   //100 "bbb" undefined "aaaa" undefined "BBB" "AAA"

Subclass override superclass method

            funA(){
                console.log("FUNAB...")
            }
        b.funA();//FUNAB...
        b.__proto__.constructor.__proto__.prototype.funA();//FUNA
        b.__proto__.__proto__.funA();//FUNA

Seeing this, I believe you are very clear.



Comments

Popular posts from this blog

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.

Use of custom events in Vue components.

 The parent component uses props to pass data (such as a method) to the child component, and the child component needs to use custom events to pass data back to the parent component! We can use v-on to bind custom events, and then pass them to subcomponents, which will call after receiving the method. Each Vue instance implements the Events interface, that is: use $on(eventName) to listen for events and $emit(eventName) to trigger events.

mapState, mapGetters, mapMutations, mapActions in vuex

 The above four objects are all in the vuex plug-in store, which is often used to store some data shared globally. But I still feel a little uncomfortable when using it, that is, I wrote a lot of repeated prefixes. At this time, these four objects appeared, which solved this pain point very well. mapState is mainly used to manipulate and obtain data, mapGetters is equivalent to computed properties, and mapMutations and mapActions are simplifications of mutation and action. They can be written as both objects and arrays.