9
I am studying ways to apply Object Orientation in Javascript. I realized that there are several ways to do Inheritance in Javascript. I did the one that I thought was simple and worked. But is it really within the standards? Is it correct?
Follows the Code:
function humano(){
var nome;
this.pensar = function(){
return "Estou pensando";
}
}
function animal(){
this.comer = function(){
return "Vou COMER!";
}
}
humano.prototype = new animal();
var pablo = new humano();
pablo.nome = "Pablo";
console.log(pablo.pensar());
console.log(pablo.comer());
console.log("Meu nome é " + pablo.nome);
Studying further, I have now arrived at the following code for Inheritance, would this be an advanced form of inheritance in Javascript? (Obs.: The extra properties created were to test the passage of the properties by inheritance)
var obj = {};
var coisa = {
nome: 'Rafael',
idade: '35'
};
obj.y = 55;
Object.defineProperty(obj,'x',{value: 1, writable: false, enumerable: false, configurable: true});
Object.defineProperty(obj,'x',{get: function(){ return obj.y}});
Object.defineProperty(Object.prototype,"extend",{
writable: true,
enumerable: false,
configurable: true,
value: function(o){
var names = Object.getOwnPropertyNames(o);
for(var i = 0; i < names.length; i++ ){
if(names[i] in this) continue;
var desc = Object.getOwnPropertyDescriptor(o,names[i]);
Object.defineProperty(this,names[i],desc);
}
}
})
obj.extend(coisa);
coisa.extend(obj);
What is the best way? Thank you
There is no "correct" form. There is the most appropriate one for each case. To make inheritance by determining that the prototype of a function is an object is common and there is nothing wrong with it. If you solved the problem for your example simply, then it is valid.
– Oralista de Sistemas
Just one more thing. The variable
nome
in functionhumano
is lost forever after the function is performed. It is not part of the human prototype, nor of a closure, nor the returned object. When you call your human Pablo, you create a new property in the object. Then you can discard thisvar nome
hassle-free.– Oralista de Sistemas
Search for be in no hurry to accept an answer. I was about 10 minutes writing a very complete one, but I don’t think I’ll even post more... P
– mgibsonbr
Are we here to get our answers accepted? Or to help the community? But I’m still grateful for your 10 minutes @mgibsonbr. Thank you very much
– Michael Alves
@Michaelalves There is good material right here at Sopt explaining heritage in Javascript. I was compiling and contextualizing your particular case. But if you’re satisfied with what you’ve received so far, there’s no point in me continuing. I leave this link as a reference if you want to know more: http://answall.com/q/15239/215 P.S. This is also good: http://answall.com/q/30718/215
– mgibsonbr
@mgibsonbr I would like to see your answer! Although I agree that one should wait a while to accept a kkkk answer
– Renato Gama
@mgibsonbr I was also curious as to what you were going to say... It would happen to have to do with what I put in my reply?
– bfavaretto
@mgibsonbr I also got curious :P If you are still in the cache put. Your answers are usually worth reading!
– Sergio
The question is not my answer, I was just indicating to AP that he could generally receive better answers if he waited longer to accept one (it depends on the type of question). Not so much because of the possibility of the answer being accepted, but because it is an indication that the PA is satisfied and may not even return to that question. As for what I was going to write, it’s no big deal, if it was I would have gone ahead and posted anyway... :)
– mgibsonbr
@bfavaretto Your answer partly covers what I was going to say (less memory is spent putting the methods in the prototype than in the instance itself, if there are many objects), and the other part refers to the use of the
var nome
in the constructor - which is not incorrect (there are use cases for this) but it would have a different effect than intended. It was an explanation of how the prototyping chain works - something that can be found in the other referenced questions.– mgibsonbr