An example of the inheritance in this case are the character classes:
we have the "father" class, which is Game_CharacterBase, and its definition:
function Game_CharacterBase() {
this.initialize.apply(this, arguments);
}
Object.defineProperties(Game_CharacterBase.prototype, {
x: { get: function() { return this._x; }, configurable: true },
y: { get: function() { return this._y; }, configurable: true }
});
Then we have Game_Character, which is the "son" of Game_CharacterBase:
function Game_Character() {
this.initialize.apply(this, arguments);
}
Game_Character.prototype = Object.create(Game_CharacterBase.prototype);
Game_Character.prototype.constructor = Game_Character;
And then we have Game_Player and Game_Event, which are "sons" of Game_Character:
function Game_Player() {
this.initialize.apply(this, arguments);
}
Game_Player.prototype = Object.create(Game_Character.prototype);
Game_Player.prototype.constructor = Game_Player;
//---
function Game_Event() {
this.initialize.apply(this, arguments);
}
Game_Event.prototype = Object.create(Game_Character.prototype);
Game_Event.prototype.constructor = Game_Event;
You see? What I meant is that you can have your own "father" class that has a function, and a "son" class that has the father function and a new one, and "grandchildren" that has all the functions.
Anyway, if you just want to add something in Object, i think it would work as the following:
Object.defineProperties(Object.prototype, {
example: { get: function() { return true; }, configurable: false }
});
Not sure it this works tho...
First things first i tried your way to add properties to objects and it works so really thank you for that it will be very usefull.
For the inheritance intead i didnt' explain very well. In languages li C# you can inherit from varius classes that are not related with eachother like:
normal inheritance multiple inheritance
granpa father mother
| \ /
father son
|
son
To be more specific i want to create 2 classes, WorldSenses which contains the value of the senses like sight, and WorldPresence which contains the "presence" of something like visibility or how much he smells. Each of these classes have also their own methos for retrieving values etc.
At this point i want ot create the class WorldEntity which contains both classes, like a living being that can sense but can also be perceived. To be fair i can just create WorldEntity mixing the other 2 classes by hand by i would like to have them separated because i could use them alone like for example a sentry camera that can see thing but doesn't need to be perceived by other things, or for example a sadwitch which can be seen and smelled but doent' need to perceive by himself.
Oh right. I don't think there's multiple inheritance in Javascript, but you can use roles:
Class WorldSenses -> has light sensitive functions
Class WorldPresence -> has smelling functions
Class WorldEntity -> Can sense light and smells
has instance of WorldSenses
has instance of WorldPresence
Class LightEntity -> Can only sense light
has instance of WorldSenses
Class SmellEntity -> Can only smell
has instance of WorldPresence
yeah i though about that, in the end for convinience reasons i used WorldEntity as the base class and the other two inherit from it and then forces values they dont' use (like sight for a presence) to 0. Thank you for your help!
EDIT: I found a solution! I did it like this:
// INTERFACE 1
function Isensor(sensor) {
this.sensor = value;
this.isSensor = function() { return this.sensor !== undefined }
}
// INTERFACE 2
function Ibody(body) {
this.body = value;
this.isBody = function() { return this.body !== undefined }
}
// Class that inherit both
function Entity(sensor, body) {
Isensor.call(this, sensor);
Ibody.call(this, body);
}
The result is a combination of the both classes