首页 > 解决方案 > 如何从作为类/对象一部分的外部函数中存在的内部函数获取数据?

问题描述

我正在学习如何使用 JavaScript 创建对象和类,我很抱歉在代码中创建了太多注释。这是为了帮助我记住重要的概念。我对 JavaScript 很陌生!我的问题是,我试图从类或对象外部访问函数,但如果没有语法错误,我似乎无法做到这一点。我假设是因为变量或方法超出了范围。那么如何调用类中存在的方法?我希望对象显示我创建的任何对象的周长。请帮忙!

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

标签: javascriptfunctionobject

解决方案


不确定您的类的逻辑,但这是您的代码的工作版本,具有正确的语法和函数,可以访问对象的内部上下文,并且可以从对象外部访问:

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());

只需单击Run code snippet即可查看它的实际效果


推荐阅读