1

Is there any way to dynamically add a constructor to a Class without altering the original Class itself?

I'm using a library where I instantiate objects like so:

var path = new Path()

What I want to do is something like this:

// obviously won't work but you get the point
Path.prototype.constructor = function() {
  console.log(this, 'was created')
}

var path = new Path()

I can obviously use a Factory to create my objects and in that Factory I can add a custom function and trigger it. That won't work since the library I'm using won't be using that factory internally.

3
  • might help you gist.github.com/parwat08/94071d1ba6df156d5e3b2436b250c13b Commented Feb 4, 2018 at 5:04
  • I'm not entirely sure about your context, could you extend the class? Commented Feb 4, 2018 at 5:52
  • @JesseKernaghan No. If I extend the Class, the library internals won't be using my extended Class. Commented Feb 4, 2018 at 5:53

1 Answer 1

1

Yes, you can, but the variable Path needs to be non-const in order for it to work. This approach will require you to still call the original constructor:

Path = class extends Path {
  constructor () {
    super()
    console.log(this, 'was created')
  }
}

class Path {
  constructor () {
    console.log('old constructor')
  }
  
  foo () {
    console.log('bar')
  }
}

Path = class extends Path {
  constructor () {
    super()
    console.log(this, 'was created')
  }
}

let path = new Path()
path.foo()

You can also replace Path with a new class identical to the original except for the constructor:

Path = (Path => {
  return Object.setPrototypeOf(
    Object.setOwnPropertyDescriptors(function () {
      console.log(this, 'was created')
    }, Object.getOwnPropertyDescriptors(Path)),
    Object.getPrototypeOf(Path)
  )
})(Path)

class Path {
  constructor () {
    console.log('old constructor')
  }

  foo () {
    console.log('bar')
  }
}

Path = (Path => {
  return Object.setPrototypeOf(
    Object.defineProperties(function () {
      console.log(this, 'was created')
    }, Object.getOwnPropertyDescriptors(Path)),
    Object.getPrototypeOf(Path)
  )
})(Path)

let path = new Path()
path.foo()

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.