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

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.