Objects with For/In

Asked

Viewed 143 times

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]();
    }
}

1 answer

3


You can use Object.defineProperty() with enumerable: false,, and this will hide the property of iterators.

function CarroAspects(modelo, chassi, qtdPortas) {
  this.modelo = modelo;
  this.chassi = chassi;
  this.qtdPortas = qtdPortas;
}
Object.defineProperty(CarroAspects.prototype, 'andar', {
  enumerable: false,
  configurable: false,
  writable: false,
  value: function() {
    console.log('vrum vrum');
  }
});
const carroPrototipo = new CarroAspects('protótipo', '1290381209', 2);
for (var carroProperty in carroPrototipo) {
  console.log(typeof carroPrototipo[carroProperty]); // var dar: string, string, number Objeto
}

console.log('Vamos dar uma volta?');
carroPrototipo.andar(); // vai dar : vrum vrum


Edit after Edit in question:

If I understand correctly you must wear one getter to do what you want. Again using the same technique (Object.defineProperty), but with a variant:

function CarroAspects(modelo, chassi, qtdPortas) {
  this.modelo = modelo;
  this.chassi = chassi;
  this.qtdPortas = qtdPortas;
}

function vrumVrum() {
  return 'vrum vrum';
}
Object.defineProperty(CarroAspects.prototype, 'andar', {
  enumerable: true,
  get() {
    return vrumVrum();
  }
});
const carroPrototipo = new CarroAspects('protótipo', '1290381209', 2);
for (var carroProperty in carroPrototipo) {
  console.log(carroPrototipo[carroProperty]); // var dar: protótipo, 1290381209, 2, vrum vrum
}

  • 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}
 });

  • 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 ..

  • 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...

  • @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.

  • 1

    the first code is right. gave the expected result.. !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.