3
I have the class Animal
that has some properties that are enumerable: true
, that is, I can list them via Reflection using for..in
.
So I have the class Coelho
that has the attribute cor
. How rabbit inherits from Animal
via prototype
, I can call Function Descrever()
. It turns out that the color rabbit is not listed in Reflection.
What am I doing wrong? What is the possible solution?
Code working on Jsbin here: http://jsbin.com/cuyaci/1/edit?js,console,output
console.clear();
var Animal = function(nome,comida){
var _nome=nome||'';
var _comida=comida||'';
var self=this;
Object.defineProperty(this,"Nome",{
get:function(){return _nome;},
set:function(value){_nome=value;},
enumerable:true
});
Object.defineProperty(this,"Comida",{
get:function(){return _comida;},
set:function(value){_comida=value;},
enumerable:true
});
this.Descrever=function(){
for(var prop in self)
if(typeof self[prop]!=='function')
console.log(self[prop]);
};
};
var Coelho = function(cor){
this.cor=cor||"Branco";
};
Coelho.prototype=new Animal('coelho','legumes');
var a = new Animal("vaca",'grama');
var c = new Coelho('azul');
a.Descrever();
c.Descrever();
I at least am curious for further information :D
– Rui Pimentel
The explanation is that not only do you need to assign a parent class instance to the
prototype
of the daughter, but in addition invoke the father builder, which is not invoked automatically. Maybe this post will help more; https://www.exratione.com/2011/05/inheritance-and-initialization-in-nodejs/– Renato Gama