vue-class-store
vue-class-store copied to clipboard
Issues with wrapped constructors
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.