Search the Community
Showing results for tags 'es6'.
Found 1 result
-
With ES6 classes, trying to extend the parent class in plugins can lead to its children still using the old parent codes. For instance, you can place this in a JavaScript sandbox and verify the console output yourselves: So normally, you'll still have to directly type prototypes: But I wanted to offer an alternative for those not being familiar with ES5 or avoiding prototypes like a plague despite the fact that many RMMZ codes are written that way, and this is what I've come up with: I've tested that it works for adding new instance variables and prototype methods in base classes, and extending and overriding existing prototype methods there(you can place the linked snippet into a JavaScript sandbox and verify the console output yourselves). While I failed to inherit the changes in the static functions of the base classes from plugins as well, this can be easily mitigated by using BaseClass.staticFunc.call(this) instead of super.staticFunc(). Basically, the essence of the issue when aliasing ES6 class inheritance without direct prototyping is this: 1. The original child class inherits the original base class 2. A plugin extends the original base class to alias some of its prototype methods 3. The child class still inherits the original base class So to solve this, simply store the linkage between the child class and the base class right after creating that child class, then points the parent of the child class to the extended base class right after extending it. As for the static functions in classes, while I tried to use the linkage to let the exiting children class use the new static functions from the extended parent class, I failed to cover the case for aliasing existing parent class static functions, because it's just impossible: 1. The super in the static functions of the child class always points to the original parent class 2. The super in the static functions of the extened parent class always points to the original parent class 3. The super in the static functions of the child class's supposed to always point to the extended parent class Clearly, combining 1 and 2 will contradict with 3, which is the goal I've trying to achieve. For those not being familiar with ES5 or avoiding prototypes like a plague, I hope using ExtendedClassAlias won't be too demanding for you, as all you need to do is sticking to these: /* * Do these 2 additional things when using ES6 class inheritance aliasing * without directly typing prototypes: * 1. Add the following code right below a new class inheriting another one: * - ExtendedClassAlias.inherit(Klass); * Where Klass is the new class inheriting another one * 2. Add the following code right below extending an existing class as a way * to alias its methods: * - ExtendedClassAlias.updateClass(Klass); * Where Klass is the existing class being extended as a way to alias its * methods * Right now it doesn't work well with inheriting static functions in classes, * so those in children classes should use ParentClass.staticFunc.call(this) * instead of super.staticFunc() */ P.S.: I've spent almost 24 hours on this and I enjoyed the process a lot, even though this might not be practical enough to be used in MZ :)