简体   繁体   English

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

[英]How can I call a function from another function inside a Class

I have the following problem: I want to call the function printHello from the function testHello .我有以下问题:我想调用的函数printHello从功能testHello The printHello function works on its own, however, when I try to call printHello from the testHello function, I get a reference error. printHello函数独立工作,但是,当我尝试从testHello函数调用printHello ,出现引用错误。 Thank you for your help.感谢您的帮助。


 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

Use this keyword.使用this关键字。 Also, you had a few bugs (i commented them)另外,你有一些错误(我评论了它们)

 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

You probably need to call你可能需要打电话

this.printHello(this.name);

inside your testHello Function.在您的 testHello 函数中。

A couple of issue in the code that can be easily fixed.代码中的几个问题可以轻松修复。

1) You need to set the this.name property in the constructor. 1) 您需要在构造函数中设置this.name属性。

2) You want to prefix the call to printHello within testHello with this. 2) 您想在testHelloprintHello的调用添加前缀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();

 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 is defined on the global level. var test 是在全局级别定义的。 This returns an object that contains name property.这将返回一个包含 name 属性的对象。 In the prototypes, we can see constructor, printHello and testHello functions.在原型中,我们可以看到构造函数、printHello 和 testHello 函数。 Now when you call //test.printHello// it will work without a problem.现在,当您调用 //test.printHello// 时,它将毫无问题地工作。 Why?为什么? when the function is not at the same level, then it will go down to prototypes until it meets the function.当函数不在同一层级时,就会下到原型,直到遇到函数。 In the prototype, you can see the printHello function.在原型中,您可以看到printHello 函数。 This is call prototype inheritance.这就是调用原型继承。

Now what happens in //test.testHello()// .现在在 //test.testHello()// 中发生了什么。 javaScript trying to execute this one and see another function inside that. javaScript 试图执行这个并查看其中的另一个函数。 So it will try to find where this printHello function defined!.所以它会尝试找到这个 printHello 函数定义的位置!。

Now we need to know about the lexical environment.现在我们需要了解词法环境。 Check this sample,检查这个样本,

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

Now what will happen if you call foo()?现在如果你调用 foo() 会发生什么? it will console.log 'world',它将 console.log 'world',

Now I'm removing test var inside the function现在我正在删除函数内的 test var

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

Now let's call foo();现在让我们调用 foo(); The output will be 'hello'输出将是“你好”

  1. When the function executes, javascript trying to find the test variable.当函数执行时,javascript 试图找到测试变量。 Oh, it's on the same level.哦,是同一个层次。 In other word functional level.换句话说,功能级别。 Now javaScript know the value of test so it will execute console.log();现在javaScript 知道test 的值,所以它会执行console.log();
  2. This time javascript try to find test variable.这次 javascript 尝试查找测试变量。 mmm, it's not inside the same level.嗯,不在一个层次。 (functional level). (功能级别)。 Ok, no problem.好的,没问题。 Let see where is this function defined.让我们看看这个函数在哪里定义。 Oh, it's defined on the global level.哦,它是在全球范围内定义的。 Ok, now I'm gonna find this test var at the global level.好的,现在我要在全球范围内找到这个测试变量。 ah ha, here it is.啊哈,来了。 its value is 'hello'它的值是“你好”

The same thing happens with your code.你的代码也会发生同样的事情。 Once javascript trying to execute test.testHello(), it sees printHello() inside that function.一旦 javascript 尝试执行 test.testHello(),它就会在该函数中看到 printHello()。 Now it will try to find the function at the same level.现在它将尝试找到同一级别的函数。 It is not defined in the testHello() function neither inside prototypes of testHello().它没有在 testHello() 函数中定义,也没有在 testHello() 的原型中定义。 So now javaScript trying to find out where is this testHello() function defined.所以现在 javaScript 试图找出这个 testHello() 函数在哪里定义。 It is on the global level because var test is defined on the global level.它在全局级别,因为 var test 是在全局级别定义的。 Is this printHello() defined at the global level?这个 printHello() 是在全局级别定义的吗? NO!不! It is inside the prototype of that object.它位于该对象的原型中。 Now javaScript cannot find the printHello function in global level either.现在javaScript 也找不到全局级别的printHello 函数。 So it will throw an error that saying printHello() is undefined.所以它会抛出一个错误,说 printHello() 是未定义的。 'this' keyword help you to refers to the object it belongs to. 'this' 关键字帮助您引用它所属的对象。 Simply, with 'this' keyword we are asking please check the test object's prototype before search on the global level.简单地说,使用“this”关键字,我们要求在全局级别搜索之前检查测试对象的原型。 (notice that not in the testHello() prototypes but in the test object prototype) (注意不是在 testHello() 原型中,而是在测试对象原型中)

Something important to understand is functions are not actually functions.需要理解的重要一点是函数实际上并不是函数。 They are objects in javaScript!!!它们是 JavaScript 中的对象!!! Use 'this' keyword.使用“this”关键字。 It will help.我会帮你的。 Also, try to learn about prototype inheritance and lexical environment.此外,尝试了解原型继承和词法环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从对象内部调用类函数? - How can I call a class function from inside an Object? 如何从同一类中的另一个函数调用函数 - How to call function from another function inside the same class 如何在 javascript 类中的另一个函数中调用异步函数? - How can i call an async function inside another function in a javascript class? 如何从 Ajax 在打字稿中调用的函数内部调用类中的函数? - How can I call a function inside a class from inside a function called up by Ajax in typescript? 如何在jquery的另一个函数中嵌入函数调用? - How can I embed a function call inside another function in jquery? I can't call a class function inside another class function in Javascript - I can't call a class function inside another class function in Javascript 如何从另一个 function 调用 function 以及如何? - How can I call a function from another function and how? 如何从 Angular 6 中的另一个类调用一个类中的函数? - How do I call a function in a class from another class in Angular 6? 如何从 reactjs 中的另一个 function 呼叫一个 function? - How can I call a function from another function in reactjs? 如何在Node.js中从另一个函数调用一个函数 - How can I call one function from another function in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM