Home > database >  How to run an object method without it showing up in the object?
How to run an object method without it showing up in the object?

Time:10-13

I need to create an object that holds properties of birds. I then need to be able to use object methods to add new values to this object, without the methods being inside the object. I've been able to output what I'm supposed to, BUT only with the object methods inside the object. I'm don't know how I can apply an object outside of the object, to the properties within the object?

function Bird(species, color, locations) {
  let birdObject = {    species : species,
    color : color,
    locations : locations,
    getColor : function() {
      return this.color;
    },
    setColor : function(newColor) {
      this.color = newColor;
    },
    addLocation : function(newLocation) {
      this.locations.push(newLocation);
    },
    getLocations : function() {
      return this.locations;
    }
  }
  return(birdObject);
}

const newBird = Bird('canary', 'red', ['newyork', 'spain'])

console.log(newBird)
console.log(newBird.getColor());
newBird.setColor('yellow');
console.log(newBird.getColor());
newBird.addLocation('france');
console.log(newBird.getLocations())

CodePudding user response:

I believe you may be interested in an object-oriented style (using the new keyword). Overall using Object.defineProperties can help to prevent methods from appearing under enumeration.

function Bird(species, color, locations) {
  Object.assign(this, {
    species,
    color,
    locations
  });
};
Object.defineProperties(Bird.prototype, {
  getColor:     { enumerable: false, value: function() { return this.color; } },
  setColor:     { enumerable: false, value: function(color) { this.color = color; } },
  addLocation:  { enumerable: false, value: function(loc) { this.locations.push(loc); } },
  getLocations: { enumerable: false, value: function() { return this.locations; } }
});

const bird = new Bird('canary', 'red', ['newyork', 'spain'])

console.log(bird)
console.log(bird.getColor());
bird.setColor('yellow');
console.log(bird.getColor());
bird.addLocation('france');
console.log(bird.getLocations());

Note that you can omit enumerable: false, because if omitted it defaults to false. I only included it to make the behaviour of Object.defineProperties appear more explicit.

Note that this stops the properties from "showing up on the object" in some contexts but not all.

CodePudding user response:

Oh, figured out what I needed to do. I created a second object that stored the methods I needed. I then passed that object (birdFunc) into the Object.create method and stored that created object in birdObj. They just wanted our class to use Object.create so I just looked up how to use it.

  function Bird(species, color, locations) {
  const birdObj  = Object.create(birdFunc);
  birdObj.species = species;
  birdObj.color = color;
  birdObj.locations = locations;
  return birdObj;
}

const birdFunc = {
  getColor : function() {
    return this.color;
  },
  setColor : function(newColor) {
    this.color = newColor;
  },
  addLocation : function(newLocation) {
    this.locations.push(newLocation);
  },
  getLocations : function() {
    return this.locations;
  }
}
  • Related