9

I'm trying to access static method baseArea from parent class cars but it shows following error:

test.php:34 Uncaught TypeError: (intermediate value).baseArea is not a function
    at Bike.get bikeArea [as bikeArea] (test.php:34)
    at test.php:42

But it works fine when I use baseArea () {} instead of static baseArea() {}

What am I doing wrong?

class Cars {
    constructor(x, y) {
        this.height = x;
        this.width = y;
    }

    static baseArea() {
        return 44;
    }
}

class Bike extends Cars {
    constructor(flag) {
        super(flag, flag);
    }

    get bikeArea() {
        return super.baseArea();
    }
}

let bike = new Bike(10);
console.log(bike.bikeArea);
7
  • 3
    Static functions you usually don't call on the object. you call them on the class. Commented Aug 18, 2017 at 12:29
  • 1
    super.baseArea() should be Cars.baseArea(). Commented Aug 18, 2017 at 12:29
  • or Bike.baseArea() Commented Aug 18, 2017 at 12:30
  • Please see the example under Super class calls with super here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… They've also called static method using super How's that working? Commented Aug 18, 2017 at 12:41
  • It may be because the pingpong method is also static AND called with Computer.pingpong() and not new Computer().pingpong() The whole chain is static. Maybe in that circumstances it succeed to resolve the super. Commented Aug 18, 2017 at 12:45

4 Answers 4

10

It does not work because super. is referencing a class instance. And a static method are not attached to instances but to class themselves.

However, the following will work:

class Cars {
    constructor(x, y) {
        this.height = x;
        this.width = y;
    }

    static baseArea() {
        return 44;
    }
}

class Bike extends Cars {
    constructor(flag) {
        super(flag, flag);
    }

    get bikeArea() {
        return Bike.baseArea();
    }
}

Note the Bike.baseArea() (which for the sake of readability can be called that way : Cars.baseArea()).

In the example that you linked here, it is likely that it works because the pingpong method is also static AND called with Computer.pingpong() and not new Computer().pingpong() The whole chain is static. Maybe in that circumstances it succeed to resolve the super.

Sign up to request clarification or add additional context in comments.

1 Comment

See my comment.
10

To call parent's static method on child's non-static method using super keyword

class Bike extends Cars {
    get bikeArea() {
        return super.constructor.baseArea();
    }
}

However, to call parent's static method on child's static method using super keyword

class Bike extends Cars {
    static bikeArea() {
        return super.baseArea();
    }
}

1 Comment

I don't like this type of comments usually, but this is definitely the most accurate answer and should be the approved answer. Thank you.
3

You can simply call Bike.baseArea() instead of bike.bikeArea() as it has been extended to include Cars.

or

You could change,

get bikeArea() {
    return super.baseArea();
}

to,

static bikeArea() {
    return super.baseArea();
}

Thereby allowing you to call it as Bike.bikeArea().

1 Comment

See my comment under question.
3

You can chnage your

get bikeArea(){
   return super.baseArea();
}

to

get bikeArea(){
   return Cars.baseArea();
}

Or you can simply use that:

class Cars {
    constructor(x, y) {
        this.height = x;
        this.width = y;
    }

    baseArea() {
        return 44;
    }
}

class Bike extends Cars {
    constructor(flag) {
        super(flag, flag);
    }

    bikeArea() {
        return super.baseArea();
    }
}

let bike = new Bike(10);
console.log(bike.bikeArea());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.