Reputation: 858
// make object properties private
var Bike = function() {
var speed = 100; // private variable
function addUnit(value) { // private function
return value + "KM/H";
}
this.getSpeed = function () { // public function
return addUnit(speed);
};
};
var myBike = new Bike();
console.log(myBike);
I'm doing the 'Free Code Camp" thing and this is one of the exercises. Other than the console.log(myBike);
this is the solution to their exercise. However, printing out the data isn't part of the exercise and I like to print out the data. So I'm running this code in node.js and I can't get the data to print. When I run my file I get { getSpeed: [Function] }
. What I think it should be is 100 KM/H
. Could someone show me how to print out the data?
Upvotes: 0
Views: 159
Reputation: 13409
myBike
is an object of type Bike
, so what you're printing is actually the instance of your "class" called Bike, you need to do
console.log(myBike.getSpeed())
which is to say "invoke the getSpeed method which exists on my Bike object"
Upvotes: 2