【问题标题】:How can I call a function from another function inside a Class如何从类中的另一个函数调用函数
【发布时间】:2019-04-03 13:34:45
【问题描述】:

我有以下问题:我想从函数testHello调用函数printHello。 printHello 函数独立工作,但是,当我尝试从 testHello 函数调用 printHello 时,我得到一个引用错误。感谢您的帮助。


class Test {
  constructor(name) {
    this.name;
  }

  printHello(parameter) {
    console.log(parameter);
  }

  testHello() {
    printHello(printHello(this.name));
  }

}
var test = new Test("Sandro");
test.printHello("hello"); //works, prints "Hello" to the Console 
test.testHello(); // does not work: Reference Error: printHello is not defined

【问题讨论】:

  • this.printHello()
  • 在printHello 中调用printHello 会得到undefined,如果你不在构造函数中初始化名字也是一样的
  • 一个关于课程如何运作的简单网络搜索会自己回答这个问题

标签: javascript function referenceerror


【解决方案1】:

使用this 关键字。另外,你有一些错误(我评论了他们)

class Test{
    constructor(name){
        this.name = name; // <- you need to assign the `name` to `this.name`
    }

    printHello(parameter){
        console.log(parameter);
    }

    testHello(){
        this.printHello(this.name); // <- you had double invocation here
    }

}
var test = new Test("Sandro");
test.printHello("hello");   //works, prints "Hello" to the Console 
test.testHello();  // does not work: Reference Error: printHello is not defined

【讨论】:

    【解决方案2】:

    你可能需要打电话

    this.printHello(this.name);
    

    在你的 testHello 函数中。

    【讨论】:

      【解决方案3】:

      代码中有几个问题很容易解决。

      1) 需要在构造函数中设置this.name属性。

      2) 你想在testHello 中给printHello 的调用加上this. 前缀

      class Test {
        constructor(name) {
          //You need to actually set this.name to the parameter value
          this.name = name;
        }
      
        printHello(parameter) {
          console.log(parameter);
        }
      
        testHello() {
          //this keyword is needed to prefix the method name
          this.printHello(this.name);
        }
      }
      
      //Now below should work with no issues
      var test = new Test("Sandro");
      test.printHello("hello");
      test.testHello();

      【讨论】:

        【解决方案4】:

        class Test {
          constructor(name) {
            this.name = name;
          }
        
          printHello(parameter) {
            console.log(parameter);
          }
        
          testHello() {
            this.printHello(this.name);
          }
        
        }
        var test = new Test("Sandro");
        test.printHello("hello"); //works, prints "Hello" to the Console 
        test.testHello(); // does not work: Reference Error: printHello is not defined

        var test 是在全局级别定义的。这将返回一个包含名称属性的对象。在原型中,我们可以看到构造函数、printHello 和 testHello 函数。现在,当您调用 //test.printHello// 时,它将毫无问题地工作。为什么?当函数不在同一个层次时,就会下到原型,直到满足函数。在原型中,可以看到 printHello 函数。这就是调用原型继承。

        现在 //test.testHello()// 会发生什么。 javaScript 试图执行这个并查看其中的另一个函数。所以它会尝试找到这个 printHello 函数定义在哪里!。

        现在我们需要了解词法环境。检查此示例,

            var test = 'hello' ; 
            function foo(){
               var test = 'world'
               console.log(test)
            }
        

        现在如果你调用 foo() 会发生什么?它会 console.log 'world',

        现在我正在删除函数内的测试变量

            var test = 'hello' ; 
            function foo(){
               console.log(test)
            }
        

        现在让我们调用 foo();输出将是“你好”

        1. 当函数执行时,javascript 试图找到测试变量。哦,是同一个级别的。换句话说,功能级别。现在 javaScript 知道了 test 的值,所以它会执行 console.log();
        2. 这次 javascript 尝试查找测试变量。嗯,它不在同一层内。 (功能层面)。好的,没问题。让我们看看这个函数是在哪里定义的。哦,它是在全球层面上定义的。好的,现在我要在全球范围内找到这个测试变量。啊哈,来了。它的值是“你好”

        您的代码也会发生同样的事情。一旦 javascript 尝试执行 test.testHello(),它会在该函数中看到 printHello()。现在它将尝试在同一级别查找函数。它既没有在 testHello() 函数中定义,也没有在 testHello() 的原型中定义。所以现在 javaScript 试图找出这个 testHello() 函数是在哪里定义的。它在全局级别上,因为 var test 是在全局级别上定义的。这个 printHello() 是在全局级别定义的吗?不!它位于该对象的原型中。现在 javaScript 也无法在全局级别找到 printHello 函数。所以它会抛出一个错误,说 printHello() 是未定义的。 'this' 关键字帮助您引用它所属的对象。简单地说,我们要求使用“this”关键字在全局级别搜索之前检查测试对象的原型。 (注意不是在 testHello() 原型中,而是在测试对象原型中)

        需要理解的重要一点是函数实际上并不是函数。它们是javascript中的对象!!!使用“this”关键字。我会帮你的。另外,尝试了解原型继承和词法环境。

        【讨论】:

          猜你喜欢
          • 2015-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-09
          • 1970-01-01
          • 2013-08-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多