Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with wrapped constructors #30

Open
thecodewarrior opened this issue Nov 4, 2021 · 0 comments
Open

Issues with wrapped constructors #30

thecodewarrior opened this issue Nov 4, 2021 · 0 comments

Comments

@thecodewarrior
Copy link

Background

@VueStore wraps the class's constructor in a new function, but this leads to several issues:

  • Static properties and methods are no longer accessible
  • store instanceof StoreClass returns false
  • The constructor name is always construct. This isn't a huge problem, but it would be a nice-to-have.

Implementation

Statics

In ES6, classes inherit statics by defining the superclass constructor as their prototype. It makes sense to me to do the same, so we just have to do Object.setPrototypeOf(wrapper, constructor)

instanceof

Solving this issue is also straightforward. The instanceof operation just checks to see if the constructor's prototype is part of the instance's prototype chain, so we just have to set wrapper.prototype = constructor.prototype

Constructor name

Setting a function name dynamically isn't as straightforward, but it's not terribly complex. (See this answer on StackOverflow.) Note that we can't use the { [name]() {...} } syntax because functions declared with the ES6 class syntax aren't constructable. Instead we use { [name]: function() {...} }, which still sets the name but is also constructable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant