0

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
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Robert L
  • 27
  • 3
  • Why is `theRadius` declared within `theDiameter`? Doesn't make sense. – Bergi Aug 21 '18 at 16:06
  • Yes, when using a plain function call, `this` is undefined. You would need to call it as a method, or use an arrow function. – Bergi Aug 21 '18 at 16:07
  • "*I'm trying to access the functions from outside the class or object*" - can you please add the code that is instantiating a `ball` and tries to access (call?) the functions? – Bergi Aug 21 '18 at 16:09
  • I was creating the object with: let basketBall = new ball (); and then calling it with: basketBall.ballInfo.displayDiameter(6); but I kept getting an error as follows: Uncaught ReferenceError: diameter is not defined at ball.get ballInfo [as ballInfo] (index3.js:136) at index3.js:202 – Robert L Aug 22 '18 at 18:56
  • Yes, when evaluating the `basketBall.ballInfo` getter the line `let displayDiameter = theDiameter(diameter)` does throw that error because the argument `diameter` is not declared anywhere – Bergi Aug 22 '18 at 19:02

1 Answers1

0

Not sure about the logic of your class, but here is a working version of your code, with proper syntax and functions that access the inner context of the object, and accessible from outside the object:

class Ball {
  constructor(diameter) {
    this.radius = diameter / 2;
    this.diameter = diameter;
    this.pie = 3.14;
  }

  theDiameter(diameter) {
     let circumferenceTwo = this.pie * this.diameter;
     return circumferenceTwo;
  }

  theRadius(radius) {
     let circumferenceOne = 2 * this.pie * this.radius;
     return circumferenceOne;

     if (this.radius = undefined) {
        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 class

let ball = new Ball(10);
console.log(ball.theDiameter());
console.log(ball.theRadius());

Just click Run code snippet to see it in action

Guerric P
  • 20,579
  • 2
  • 28
  • 66
  • There's a lot I got to learn, but I can definitely play around with this code before I move on to other javascript topics. Thanks! – Robert L Aug 22 '18 at 18:59