Last active
July 9, 2020 10:22
-
-
Save KinoAR/5c80fd5f8d6a244831dff0905a11f3ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================= | |
// Classes | |
//============================================================================= | |
/* | |
//============================================================================= | |
// Introduction | |
//============================================================================= | |
* Classes are a way to bundle similar functionality for a specified object. | |
* The other way to look at it is, classes are a template for some sort of object | |
* that you want to replicate. A good example is a car, a car can do multiple | |
* things such as accelerate, reverse, park, blow the horn, etc. | |
* | |
* | |
* In this case, we'll be taking a class from RPGMaker MV, and making a new class | |
* from it. In this case, we'll be making our own Window Class for drawing a title. | |
* This will be in both ES5, and ES6. | |
*/ | |
//Class Creation and Class Inheritance | |
//ES5 Version | |
function MyWindow_TitleES5(x, y, width, height) { | |
this.initialize.apply(this, arguments); | |
} | |
//Setting the constructor, and passing the methods from Window_Base to MyWindow_TitleES5 | |
MyWindow_TitleES5.prototype = Object.create(Window_Base.prototype); | |
MyWindow_TitleES5.prototype.constructor = MyWindow_TitleES5; | |
MyWindow_TitleES5.prototype.initialize = function(x, y, width, height) { | |
//Base Class Access Example (Access initialize method from the parent) | |
Window_Base.prototype.initialize.call(this, x, y, width, height); | |
this._windowName = "Title_Window"; | |
}; | |
//Setter | |
MyWindow_TitleES5.prototype.setWindowName = function(name) { | |
this._windowName = name; | |
}; | |
//Getter | |
MyWindow_TitleES5.prototype.getWindowName = function() { | |
return this._windowName; | |
}; | |
//Getter Useage Example | |
var es5window = MyWindow_TitleES5(0, 0, 200, 200); | |
console.log(es5window.getWindowName()); //Title_Window | |
//ES6 Version | |
//extends keyword passes methods from Window_Base(parent) to MyWindow_TitleES6 | |
class MyWindow_TitleES6 extends Window_Base { | |
constructor(x, y, width, height) { | |
super(x, y, width, height); | |
} | |
initialize(x, y, width, height) { | |
//Base Class Access (Accessing initialize method from parent) | |
super.initialize(x, y, width, height); | |
this._windowName = "Title_Window"; | |
} | |
//Setter | |
set windowName(name) { | |
this._windowName = name; | |
} | |
//Getter | |
get windowName() { | |
return this._windowName; | |
} | |
} | |
//Getter Useage Example | |
var es6window = new MyWindow_TitleES6(0, 0, 200, 200); | |
console.log(es6window.windowName); //Title_Window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment