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

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.