如何从Class中的另一个函数调用函数

问题描述 投票:-2回答:4

我有以下问题:我想从函数printHello调用函数testHelloprintHello函数可以自行运行,但是,当我尝试从printHello函数调用testHello时,我得到一个引用错误。谢谢您的帮助。


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
javascript function referenceerror
4个回答
5
投票

使用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

0
投票

你可能需要打电话

this.printHello(this.name);

在你的testHello函数中。


0
投票

代码中的几个问题可以轻松修复。

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

2)你想用printHellotestHello中调用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();

0
投票

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在全球范围内定义。这将返回包含name属性的对象。在原型中,我们可以看到构造函数,printHello和testHello函数。现在当你调用//test.printHello//时它会正常工作。为什么?当函数不在同一级别时,它将进入原型直到它满足该功能。在原型中,您可以看到printHello函数。这是调用原型继承。

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

现在我们需要了解词汇环境。检查这个样本,

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

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

现在我正在删除函数内的test var

    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测试是在全局级别定义的。这个printHello()是在全局级别定义的吗?没有!它在该对象的原型内。现在javaScript也无法在全局级别找到printHello函数。所以它会抛出一个错误,说printHello()是未定义的。 'this'关键字可以帮助您引用它所属的对象。简单地说,我们要问'this'关键字,请在全局搜索之前检查测试对象的原型。 (请注意,不是在testHello()原型中,而是在测试对象原型中)

重要的是要理解的是功能实际上并不是功能。它们是javaScript中的对象!使用'this'关键字。我会帮你的。另外,尝试了解原型继承和词法环境。

© www.soinside.com 2019 - 2024. All rights reserved.