-
Notifications
You must be signed in to change notification settings - Fork 2
Challenge Make Object Properties Private
Objects have their own attributes, called properties, and their own functions, called methods.
You can use the this
keyword to reference public properties and methods of the current objects. However, when You need to create private ones so they are not accessible from the outside of the object you just remove the keyword this
from the object property or method declaration and declare it with var
so that it is private outside its scope.
var Bike = function() {
var speed = 100; // private
function addUnit(value) { // private
return value + "KM/H";
}
this.getSpeed = function () { // public
return addUnit(speed);
};
};
var Cake = function() {
var loot = 2;
// Getter to know how much loot you have
this.getLoot = function() {
return loot;
};
// Setter to change the ammount of loot
this.setLoot = function(num){
loot = num;
};
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links