I want to inherit the variables created in classA constructor to ClassB and use them.

Asked 2 years ago, Updated 2 years ago, 18 views

How do I write the code to use the variables I used in ClassA in JavaScript in ClassB?

class A {
    constructor{
        This.A
    }

}

classB{
    constructor{
        This.B
    }

    method{
        // I want to use classA constructor variable in classB like this.
        console.log(this.A)<
    }
}

I want to create a flag function, so I have a value initialized in classA, and I would like to change it along with processing and execution in classB.If you create a flag in ClassB, it will be initialized with processing.I wish I could avoid it, but I can't think of it at the moment, so I'm thinking of taking this method.

javascript

2022-09-29 20:30

2 Answers

Inherited to include extensions and call super() on the constructor.

class Mover {
    constructor(x,y,imgFileName,imgOffsetX,imgOffsetY){
        This.x = x;
        This.y = y;
        this.img = new Image();
        this.img.src = imgFileName;
        this.imgOffsetX=imgOffsetX;
        this.imgOffsetY=imgOffsetY;
        This.collisions = [ ];
        This.moveX = 0;
        This.moveY = 0;
    }
    ...
}

class Player extensions Mover {
    constructor(x,y,imgFileName,imgOffsetX,imgOffsetY){
        super(x,y,imgFileName,imgOffsetX,imgOffsetY);
        This.collisions.push (new Collision (-10,0,16));
        This.collisions.push (new Collision (+16,0,16));
        This.isJumping=false;
    }
    ...
}

If there is no problem with the speed, it is recommended to follow the following rules thoroughly to make it easier to test.
·Another class used by the class is received by the constructor argument instead of new by itself (DI)
 (The above is a bad example strictly because the Collision class has been updated on its own.)
·As the above amount increases, the constructor becomes ..., so
 Consider deploying DI containers at the right time


2022-09-29 20:30

Class inheritance can be achieved.

class A {
    constructor{
        This.A
    }

}

class Bextends A {
    constructor{
        call super()//< -- constructor() on A
        This.B
    }

    method{
        console.log(this.A)<
    }
}

However, the inheritance itself can cause confusion, so I don't recommend it very much.Especially, if you repeat inheritance many times, you will not know where the variable went.If you think about whether you can use the class itself as much as possible, you will be able to design it cleanly.

Understanding Inheritance Problems

The name is protocol-chain, but what we're doing is almost the same as inheritance in a dynamic typing language.As I said, "I don't know where the variable went," it causes namespace confusion.This is a thorny issue for beginners and newcomers to big projects.

Also, although I did not mention it in my answer, there is a problem that "repeated succession may increase the class, but not decrease the class."The properties will increase more and more, making it difficult for object users to understand.For example, a function that takes B as an argument that inherits A must also check properties in case A is passed incorrectly.The programmer is also responsible for checking properties in dynamic typing languages.If you don't check, it's no longer a safe program.

And this causes the problem of differential programming.Differential programs are the idea that only additional functions can be implemented by inheriting classes.At first glance, it seems to be the ideal way to do it.This means that you can't read the full property of the final object just by looking at the implementation of the inheritance destination.class is still good, but source code written with a lot of protocol-chain is very hard to follow.

It's not "It's OK to do it because it's official."Succession is a troubling concept that causes a variety of problems if not carefully designed.Also, the method of succession was "that was the only way that was widely known in those days, and we had no choice but to do so."You need to think about why TypeScript and other AltJS came out and compiled into JavaScript, and whether the programming language that was talked about in the 2010s does not have the equivalent of inheriting objects or the concept of objects itself.

Succession of classes and objects in JavaScript and dynamic typing languages is like magic that increases what you can do.I don't know why I like them.However, I would like to conclude by quoting these words.

"It's not when there's nothing more to add, but when there's nothing more to take away." - Antoine de Saint=Exupery


2022-09-29 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.