Freecodecamp: Waypoint: Make Object Properties Private - Instruction Clarity

Created on 4 Nov 2015  路  16Comments  路  Source: freeCodeCamp/freeCodeCamp

I have addressed a number of questions regarding this Waypoint over that past week. While I don't believe the wording of the instructions is to blame for all the confusion, perhaps some clarity could be added at the end to help users understand what needs to be done while also reinforcing the terminology. Additions in bold, deleted text struck through.

Properties Private

Objects have their own attributes, called properties, and their own functions, called methods.

In the previous challenge, we used the this keyword to reference public properties and public methods of the current object.

We can also create private properties and private methods, which aren't accessible from outside the object.

To do this, just declare properties or functions within the constructor without the this keyword.

Let's create an object with two functions. One attached as a property and one not.

See if you can keep myBike.speed and myBike.addUnit private, while making myBike.getSpeed publicly accessible.
See if you can modify the Bike constructor so that both the speed property and the addUnit method are private, while also making the getSpeed method publicly accessible for all objects created from the Bike constructor. We'll check your work by testing the myBike object we've created from the Bike constructor at the bottom of the code.

Most helpful comment

The instructions for this exercise were very confusing. I got everything that you got, @saifalizafar except for the gear = set. I was trying to do something similar to get getGear to be equal to setGear, but no go. Will try this now.

All 16 comments

Hi @MattYamamoto, we already have an issue for this at https://github.com/FreeCodeCamp/FreeCodeCamp/issues/4177.

Can you please post your suggestions on the other issue? This will ensure that they will get consideration when someone is working on fixing the issue.

Thanks!

after countless attemps i got this to pass:

 var gear = 0;

  this.setGear = function() {
    gear += 4;
  };

  this.getGear = function() {
    return gear;
  };

can someone please explain why / what happened??

hmmmm... yeah I don't think I would be able to come up with that... at least now with the "+= 4;" _sigh_

The instructions for this exercise were very confusing. I got everything that you got, @saifalizafar except for the gear = set. I was trying to do something similar to get getGear to be equal to setGear, but no go. Will try this now.

Thanks @friendofdog for the clarification. I will revisit.

@Galiante Please don't post solutions in here!

Sorry @theflametrooper , I'll delete my post :smile:

Hi friends,

I stumbled upon some videos that helped me tremendously when I was stuck on this challenge. He breaks down the concepts revolving around this exercise: Constructor functions, closures, getters, setters, etc. Have fun! and....

Keep on hacking!

Javascript Closures Part 1
Javascript Closures Part 2


bitmoji

@jrogodel Feel free to post these videos on our Subreddit!

@jrogodel ... thanks for the links, but they are the SAME... here are the correct URLs:

Video 1 = https://www.youtube.com/watch?v=DZEblYanz3c
Video 2 = https://www.youtube.com/watch?v=6ltWzDKweUQ

setGear() is just supposed to set the gear. You don't need to add new gear to the new values from set gear. all you need is x = y and not x+=y.

_removed solution by mod_

@texas2010 Thank you for the update, wasn't aware of it! That was my first comment.

@x19kutz, no problem. all good.

Thank you @jrogodel those videos did help

I just finished this. Here is what i got. I hope it helps.

removed solution by mod
Whatever we put inside the parameters of setGear() becomes the value of gear, which getGear returns and holds that value as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielonodje picture danielonodje  路  3Comments

imhuyqn picture imhuyqn  路  3Comments

robwelan picture robwelan  路  3Comments

Tzahile picture Tzahile  路  3Comments

MelissaManning picture MelissaManning  路  3Comments