【问题标题】:How to get data from a inner function that exist in an outer function that is part of a class/object? [duplicate]如何从作为类/对象一部分的外部函数中存在的内部函数获取数据? [复制]
【发布时间】:2018-08-21 16:00:02
【问题描述】:

我正在学习如何使用 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

【问题讨论】:

  • 为什么theRadiustheDiameter 中声明?没有意义。
  • 是的,当使用普通函数调用时,this 未定义。您需要将其作为方法调用,或使用箭头函数。
  • "我正在尝试从类或对象外部访问函数" - 请添加实例化 ball 并尝试访问 (调用?)函数?
  • 我正在创建对象: let basketBall = new ball ();然后调用它: basketBall.ballInfo.displayDiameter(6);但我一直收到如下错误:Uncaught ReferenceError: diameter is not defined at ball.get ballInfo [as ballInfo] (index3.js:136) at index3.js:202
  • 是的,在评估 basketBall.ballInfo getter 时,let displayDiameter = theDiameter(diameter) 行确实会抛出该错误,因为参数 diameter 没有在任何地方声明

标签: javascript function object


【解决方案1】:

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

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 sn-p即可查看它的实际效果

【讨论】:

  • 我有很多东西要学,但在我继续讨论其他 javascript 主题之前,我绝对可以玩弄这段代码。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 2013-03-23
相关资源
最近更新 更多