Closure to Protect Properties Within an Object from Being Modified Externally

Tell us what’s happening:
I tried and researched about this. I understood the concept but this is throwing the same errors. The tests cases that are not passing are " The weight property should be a private variable."

Your code so far


function Bird() {
  this.weight = 15;
  
  this.getWeight = function() {
    return this.weight;
  };
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally

I didn’t start this section but as I can see maybe you want to use

let weight = 15;

also in the function dont’use return this.weight but return weight

1 Like

Perfect. Thank you so much.