Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Created September 16, 2015 04:16
Show Gist options
  • Save chrisabrams/13d47d9b03d7cf0e8166 to your computer and use it in GitHub Desktop.
Save chrisabrams/13d47d9b03d7cf0e8166 to your computer and use it in GitHub Desktop.
How do I get `this.foo` to be assigned to class B's prototype instead of class A's prototype?
/*
How do I get `this.foo` to be assigned to class B's prototype instead of class A's prototype?
*/
class A {
constructor(options = {}) {
this.foo = options.foo
}
}
class B extends A {
constructor(options = {}) {
super(options)
}
}
@pmuellr
Copy link

pmuellr commented Sep 16, 2015

It's not on any prototypes. this.foo is the foo property of an A or B object, whichever you constructed. Obvious for the new A() that it grows a new property, but also when you new B(), the B's ctor super calls into A's ctor - with the B object as this - and so sets the foo property on the B object.

You're generally not going to be futzing with the prototypes once you have live objects, eg "assigning to prototypes".

What's the problem you're seeing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment