vue-class-store icon indicating copy to clipboard operation
vue-class-store copied to clipboard

Issues with wrapped constructors

Open thecodewarrior opened this issue 2 years ago • 0 comments

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.

thecodewarrior avatar Nov 04 '21 23:11 thecodewarrior