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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.