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

Destructuring assignment in ES6

 In the past, when we wanted to assign a value to a variable, such as an array type and an object type, to assign a value to a variable, we could only specify the value directly. It is very troublesome to write in this way, but under the ES6 syntax specification, it is allowed to directly extract the required values from arrays and objects according to a certain pattern, and directly assign variables to variables. This method is called Destructuring, which is simple to understand. That is, the left and right sides of the equal sign are equal.

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.