Use Closure to Protect Properties Within an Object from Being Modified Externally 5

Tell us what’s happening:
I am unable to figure this loop out. What am I doing wrong? I’ve looked at the hints, but nothing has helped.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 Avast/72.0.1174.122.

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

So your code makes an object like this:

{
  weight: 15,
  getWeight: function() {
    return weight
  }
}
  • There should not be a property called weight.
  • There is no variable anywhere called weight, so the function on the object will break.

Have a look at the example again: you are not doing the same thing. You should not be assigning a property called weight (this.weight is not correct)

I don’t get it. I’m sorry, my learning disability is kicking in right now, and I’m having a hard time concentrating and thinking this morning.

You’re trying to define weight in such a way that you can’t redefine it from outside of the function. Let me show you why your solution doesn’t work. The commented areas will represent the output of console.log().

Honestly, the way that you wrote it returning weight would have returned undefined, so I changed it to return this.weight so it is consistent, but otherwise this is the same as the code you wrote.

function Bird() {
  this.weight = 15;
  this.getWeight = function() {
    return this.weight;
  };
}
const blueJay = new Bird();
console.log(blueJay.getWeight();
// 15
blueJay.weight = 10;
console.log(blueJay.getWeight();
// 10

You want to write the code in such a way that weight can’t be changed from outside of the function. If you do this properly, both times it should output 10.

You need to change this.weight into a declared function. A declared function is declared using const, let or var. Then use the this.getWeight function to return that value the way you originally wrote it.

See the following,

const Dog = function Dog (color) {
  let furColor = color;
  this.getFurColor = function() {
    return furColor;
  }
}
let rover = new Dog('yellow');
console.log(rover.furColor);
// undefined
console.log(rover.getFurColor());
// yellow

Since furColor is created inside the function with a declaration rather than on the function using this.furColor, the first time you try to log it it just returns undefined. It cannot be seen from outside of the function (nor modified). However, if you use a getter function like getFurColor() you can see it that way.

So this is the example:

function Bird() {
  let hatchedEgg = 10;

  this.getHatchedEggCount = function() {
    return hatchedEgg;
  };
}

And this is your code:

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

The this.getWeight function is fine: that is the public method. But the other thing you have inside the function is not the same: you have made a publicly accessible property called weight that appears on the object you create.

I’m sorry, but your example isn’t really helpful as it confuses me badly.

So, how exactly do I change it correctly? I’m so confused.

This line in your code:

this.weight = 15;

Is not the same as the equivalent line in the example:

let hatchedEgg = 10;

this.weight = 15;

Does not do the same thing as

let hatchedEgg = 10;

this.weight is a property on an object you create from a function. let hatchedEgg is a variable defined inside a function you use to create an object.

1 Like

@DanCouper

I’m sorry, but your explanations are very confusing to me. I feel like I don’t understand what you’re trying to explain at all, and it’s hurting my brain, badly.

this.weight = 15

Is wrong, it needs to be

let weight = 15

This is why I am telling you to look at the example, your code does not follow the example

Ah, alright. Thank you! My brain isn’t working correctly today as my learning disability keeps wanting to kick in.

1 Like
const Person = function Person(firstName, lastName) {
    this.name = firstName + ' ' + lastName
}
let Bob = new Person("Robert", "Johnson");
console.log(Bob.name);
// "Robert Johnson"

In the above example, I’m making an object with the variable name defined on the object. It can be accessed or modified with Bob.name.

const Person = function Person(firstName, lastName) {
    let name = firstName + ' ' + lastName
}
let Bob = new Person("Robert", "Johnson");
console.log(Bob.name);
// undefined

In the above example, the variable name is being declared inside the function’s scope / closure. Therefore, it cannot be accessed or changed by doing Bob.name. If you want to access it, you need to create a function, usually called a “getter”, to get the value out. See the following.

const Person = function Person(firstName, lastName) {
    let name = firstName + ' ' + lastName
    this.getName = function getName() {
        return name;
    }
}
let Bob = new Person("Robert", "Johnson");
console.log(Bob.getName());
// "Robert Johnson"
1 Like