I'm learning about creating objects and classes with JavaScript and I apologize for creating too much notes within the code. It's to help me remember important concepts. I'm very new to JavaScript! My problem is, I'm trying to access the functions from outside the class or object but I can't seem to do that without getting a syntax error. I assume because the variables or methods are out of scope.Then how can I call the methods that exist in the class? I want the object to display the circumference of any object I create. Please help!
class ball {
// get method used to access properties about the class/object
get ballInfo() { // constructor name should be about the objects behavor or algorithem.
this.radius = undefined;
this.diameter = undefined; // default values for undeclared values or strings should be kept as 'undefined'. For objects use 'null' instead.
this.pie = 3.14;
// defining variables that can be used out of scope for the functions below:
let displayDiameter = theDiameter(diameter);
let displayRadius = theRadius(radius);
// function1of2:
function theDiameter(diameter) {
let circumferenceTwo = pie*diameter;
return circumferenceTwo;
// function2of2
function theRadius(radius) {
let circumferenceOne = 2*pie*radius;
return circumferenceOne;
if (this.radius = undefined) {
/* NOTE: calling this method outside of this class or object
will give you the 'unexpected identifer error' because it is out of scope!
*/
console.log("The circumference for the ball is (msg1)" + (displayDiameter(6)));
// The fact that it's not outputting the console.log statement implies that the argument is not true for the if statement
}
if (this.diameter = undefined) {
console.log("The circumference for the ball is (msg2)" + (displayRadius(4)));
}
// Unfornately I can't seem to find a way to display the diameter on the screen.
console.log("displaying the circumference using the diameter.... " + displayDiameter(6));
} // end of theRadius function
} // end of theDiameter function
} // end of ballInfo function
}; // end of class