2
I have an added function on a prototype, and I don’t want her is listed in a for/in loop, but when calling console, in the loop for/in it is displayed and not executed, so I created an "if" if the object’s data reading is a function it will execute the function (it worked). But how can I hide the reading of function on the console? I want the moment it is read, it to execute, how can I do this with a for/in loop?
Instead of displaying on the console the snippet "Function(){ console.log('vrum vrum'); }" - I wanted to hide it and display what was defined in the function - the "vrum vrum".. the question got confused. it was bad !?
function CarroAspects(modelo, chassi, qtdPortas){
this.modelo = modelo;
this.chassi = chassi;
this.qtdPortas = qtdPortas;
}
CarroAspects.prototype.andar = function(){
console.log('vrum vrum');
}
const carroPrototipo = new CarroAspects('protótipo', '1290381209', 2);
for(var carroProperty in carroPrototipo){
console.log(carroPrototipo[carroProperty]); // console -> listará Propriedades do Objeto
if(typeof carroPrototipo[carroProperty] === 'function'){ // Executará a função
carroPrototipo[carroProperty]();
}
}
Recalling that the second argument of
Object.defineProperty()
can be an object with all attributes and methods you want to add, Ex:Object.defineProperties(CarroAspects.prototype, {
 andar: {value: function() { console.log('vrum vrum');},enumerable: false},
 buzinar: {value: function() {console.log("beep beep");},enumerable: false}
 });
– Leandro Angelo
I don’t want to hide the values, I want instead of displaying -- Function()? console.log('vrum vrum'); }, display "vrum vrum"! the question got a little confusing ..
– user106463
That’s exactly what @Sergio replied, to delete the "print" of the prototype you’re creating, you need to implement it as indicated and invoke the method at the end...
– Leandro Angelo
@Brunob.Coelho added another variant to the answer. I don’t see how you want to use it, so it might get confusing to understand the answer. Say it makes sense.
– Sergio
the first code is right. gave the expected result.. !
– user106463