Hello
I'm studying javascript, but specifically Parasitic Combination Inheritance , and I came across overriding methods. It turns out that I created the following code:
var Base = function(nome, texto) {
this.name = nome;
this.text = texto;
this.saudacao = function() {
return this.text + ' ' + this.name;
}
}
var heranca = function(p, f) {
var pc = Object.create(p.prototype);
f.prototype = pc;
f.prototype.constructor = f;
}
var Dia = function(texto) {
Base.call(this, 'Fulano? ', 'Tudo bem, ');
this.newText = texto;
}
heranca(Base, Dia);
Dia.prototype.saudacao = function() {
var msg = this.saudacao();
return msg + ' ' + this.newText;
}
var ola = new Dia('Bom dia!');
alert(ola.saudacao());
When I run alert (ola.saudacao ()), I have the return of the parent class, not the override.
For testing, I changed the name of the "greetings" method of class "day" to "newSaudation" and, by running alert (new.audition ()), I get the correct output.
Apparently the code written in day.prototype.saudacao = function () {} does not even run (I added an alert ('!') on it and it did not work).
So here's the question: how to correctly work with javascript method overwriting without forgetting Parasitic Combination Inheritance ?
EDIT I will try to explain the practical application to clarify the question a bit. The objects used as examples are not in question, just their functionality.
Consider a class that calls itself "Kindness" and has the "Greetings" method.
When creating ...
var ola = new Gentileza ("José");
We can run ...
ola.Saudacao ();
And we will have: "Hello, Joseph."
Now, we need two other objects called "Day" and "Night" that are children of "Kindness." So we can create ...
var day = new Day ("Joseph");
And run ...
Now the message will be "Hello José. Good morning!". The first part of the message ("Hello, Joseph.") Was generated on the parent object (Kindness). The "Day" object inherited the greeting from the parent object and added "Good morning!" to the text.
For completion : I want my child class to inherit a parent class method and to complement it.
Thanks for your attention.