Adaddinsane
Adaddinsane

Reputation: 535

Javascript class library in closure

I'm attempting to create a JS class library in a closure for a Drupal application (the Drupal thing is not necessarily relevant but style insists on closures). I'm not a coding newbie but JS is not my area of expertise :-)

The first problem I have is that the "extends" doesn't work - I understand that I don't have the right reference for the extends class, but what should it be, because I can't find anything that works?

((window) => {

  window.lib = {

    LibClassBase: class {}

    LibClass1: class extends LibClassBase {}

  }

})(window)

Alternatively: is this just the wrong approach and should I be doing something else?

Upvotes: 0

Views: 37

Answers (1)

Quentin
Quentin

Reputation: 944441

is this just the wrong approach

Yes.

You can't read a value from an object before it has been constructed.

and should I be doing something else?

Use class declarations and create them within the scope of the closure.

Assign them to the object you are returning afterwards.

Upvotes: 1

Related Questions