Javascript Abstract Classes

Asked

Viewed 81 times

0

How can I have an abstract class and its extended classes in the same file?

for example: in forms.js

class default {
  constructor(width){
    this.width = width
  }
  area(){
    return this.width ** 2
  }
}

module.exports = class cubo extends default {
  some unique stuff for cube...
}

How can I create a cube reference in my index.js? I’ve already tried:

const formas = require('./formas.js')
const cubo = new Cubo(width)

and tried that too:

const formas = require('./formas.js')
const cubo = new formas.Cubo(width)

What I’m doing wrong?

1 answer

0


Your formas in the file you matter will be equal to module.exports of the file exporting.

Export like this:

class default {
  constructor(width){
    this.width = width
  }
  area(){
    return this.width ** 2
  }
}

class Cubo extends default {
  some unique stuff for cube...
}

module.exports = Cubo

Import like this:

const Cubo = require('./formas.js')
const cubo = new Cubo()
  • 1

    Thanks buddy, you’re the guy :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.