As MZ is now officially confirmed to be full ES6 us programmer can now use the ES6 javascript as the official javascript coding standard.
ES6? what is the differences between ES5?
Well here's a quick look at how class are defined in ES5 and ES6
Code:
// ES5 class
function Scene_Dummy() { this.initialize.apply(this, arguments); }
Scene_Dummy.prototype = Object.create(Scene_Base.prototype);
Scene_Dummy.prototype.constructor = Scene_Dummy;
Scene_Dummy.prototype.initialize = function(){
Scene_Base.prototype.initialize.call(this);
};
Scene_Dummy.prototype.createFadeSprite = function(white){
Scene_Base.prototype.createFadeSprite.call(this, white);
};
// ES6 class
class Scene_Dummy extends Scene_Base {
constructor(){
super();
this.initialize.apply(this,arguments); // should not be existing anymore for MZ but just in case
}
initialize(){
super.initialize();
}
createFadeSprite(white){
super.createFadeSprite(white);
}
}As you can see, it's make the code WAY more clean and shortened!
Although now you might ask : Do I use the same syntax when I Overwrite, Alias or extends an existing class?
The short answer is : Yes and no.
the longer answer is No since Class in ES6 are considerate final and can't be redeclared
so if you did
Code:
class Something {
}
class Something {
}It would throw an error of duplicate member.
So how do you actually extends existing class then???
The short answer : The good old MV way!
The long answer : ES6 is just syntactic sugar and in concept is still ES5 prototype class and still behave like ES5 just more 'stricter' and prettified.
so here's the method to overwrite/ alias and extends an existing class!
Code:
// alias
const alias = Scene_Base.prototype.createFadeSprite;
Scene_Base.prototype.createFadeSprite = (white) => { // Just a shortend for 'function'
alias.call(this, white);
// new content
};
// Overwrite
Scene_Base.prototype.createFadeSprite = (white) => { // Just a shortend for 'function'
// new overwritten content
};
// Extends existing class.
Scene_Base.prototype.newFunction = () => {
// content
};From @Galenmereth
He showed me how to extends existing class using ES6 syntax thanks to him!
Code:
// named function.
class Scene_Alias extends Scene_Base {
create(){
super.create();
this.something();
}
something(){
}
}
Scene_Base = Scene_Alias; // Will do the same thing than the es5 counterpart without having a global!
// anonymous class for avoid nameclashing.
Scene_Base = class extends Scene_Base {
constructor(){
super();
}
create(){
super.create();
this.something();
}
something(){
}
}EXTRA NOTE :
Every function from ES5 will still work so you can still declare class like how it worked in MV!
On That I hope it helped you and will remove any worries you had for MZ plugin development!
Happy Game dev!
On a quick note : I know it's very basic ES6 programming but for people using MZ they might google : how to extends function in MZ.