简体   繁体   English

无法从子 class - Javascript 访问父 class 属性

[英]Can't have access of parent class properties from child class - Javascript

I have Two classes in javascript and I can't get the value propertie of parent class, from the child class.我在 javascript 中有两个类,我无法从子 class 获得父 class 的值属性。

Parent class is a "Picker", the picker will get a value of the amount of oranges that the 'Picker' should grab, and put them in a array, and than call the truck.父 class 是“拾取器”,拾取器将获取“拾取器”应抓取的橙子数量的值,并将它们放入数组中,然后调用卡车。

Child class is the 'Truck', the truck receive the amount of oranges that the 'Picker' have in the array.子 class 是“卡车”,卡车收到“Picker”在数组中的橙子数量。

The problem is that, when the truck tries to get the oranges in the array, it is allways empty and I don't understand why.问题是,当卡车试图在阵列中获取橙子时,它总是空的,我不明白为什么。

This is my code这是我的代码

 class Picker { constructor() { this.orangeBox = []; } amountOrangesToPick(amount) { for (let i = 0; i <= amount; i++) { this.orangeBox.push(i); } return this; } callTruck() { console.log("TRUUUUUUCK"); console.log(this.orangeBox); } } class Truck extends Picker { constructor() { super(); console.log(this.orangeBox); } } let picker = new Picker(); picker.amountOrangesToPick(20).callTruck(); let truck = new Truck();

Thank you谢谢

You might want just to change the new Picker() to new Truck()您可能只想将new Picker()更改为new Truck()

Or you want a factory method:或者你想要一个工厂方法:

 class Picker { orangeBox = []; amountOrangesToPick(amount) { for (let i = 0; i <= amount; i++) { this.orangeBox.push(i); } return this; } callTruck() { console.log("TRUUUUUUCK"); const truck = new Truck(); truck.orangeBox = this.orangeBox.splice(0); return truck; } } class Truck extends Picker { constructor() { super(); } honk() { console.log(this.orangeBox); return this; } } const picker = new Picker(); const truck = picker.amountOrangesToPick(20).callTruck().honk(); console.log(truck.orangeBox);

But you should read about dependency injection and inversion of control, for future testing purposes.但是为了将来的测试目的,您应该阅读依赖注入和控制反转。 So, your code should look like this:因此,您的代码应如下所示:

 class Picker { orangeBox = []; truck = null; constructor(truck) { this.truck = truck; } amountOrangesToPick(amount) { for (let i = 0; i <= amount; i++) { this.orangeBox.push(i); } return this; } callTruck() { console.log("TRUUUUUUCK"); this.truck.orangeBox = this.orangeBox.splice(0); return truck; } } class Truck extends Picker { constructor() { super(); } honk() { console.log(this.orangeBox); return this; } } const picker = new Picker(new Truck()); picker.amountOrangesToPick(20).callTruck().honk();

This is how I solved my problem, using the concept of dependency injection like @Misiur said in his answer, but I did a little different, the reason I'm not accepting his answer is because I think my example is cleaner, and using dependency injection, wouldn't need to extend my classes.这就是我解决问题的方法,使用@Misiur 在他的回答中说的依赖注入的概念,但我做了一点不同,我不接受他的回答的原因是因为我认为我的例子更干净,并且使用依赖注入,不需要扩展我的课程。

class Picker {
orangeBox = [];

amountOrangesToPick(amount) {
    for (let i = 1; i <= amount; i++) {
        this.orangeBox.push(i);
    }

    return this;
}
callTruck() {
    console.log("TRUUUUUUCK");
 }
}

    class Truck {
        picker = null;
   constructor(picker) {
       this.picker = picker;
   }
   getOranges() {
        console.log(this.picker.orangeBox);
   }
}

const picker = new Picker();
picker.amountOrangesToPick(20).callTruck();

const truck = new Truck(picker);
truck.getOranges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM